\CUSDependency{ disable={titlesec,titletoc,tocloft,etoc,placeins,multitoc,minitoc}, 
  module={util},package={zhnumber} }
\CUSProvideExplModule{struct}{\cus@d@te}{\cus@versi@n}{structures of document}

%SEE: ctexheading (ctexbook.cls), titlesec, titletoc, etoc, etc. 

\tl_new:N \g__cus_cbl_before_load_tl
\tl_new:N \g__cus_cbl_after_load_tl

\keys_define:nn { cus } { cbl-setup .meta:nn = { cus/cbl-setup } {#1} }
\keys_define:nn { cus/cbl-setup } 
  {
    from .tl_gset_e:N = \g__cus_cbl_file_tl ,
    from .initial:n = \c_sys_jobname_str ,
    write .bool_gset:N = \g__cus_cbl_write_bool ,
    write .initial:n = true ,
    to  .tl_gset_e:N = \g__cus_cbl_to_tl ,
    to    .initial:n = \exp_not:N \q_no_value ,
  }
\NewDocumentCommand \enablecombinedlist { !O{} }
  {
    \if_int_compare:w \g__cus_cbl_count_int < \c_zero_int
    \cus_if_preamble_test:TF { \cus_hook_gput_ds:n } { \use:n }
      { 
        \tl_if_blank:nF {#1} { \keys_set:nn { cus/cbl-setup } {#1} }
        \g__cus_cbl_before_load_tl
        \int_gzero:N \g__cus_cbl_count_int
        \group_begin: \makeatletter
        \@input { \g__cus_cbl_file_tl .toc } 
        \group_end:
        \g__cus_cbl_after_load_tl
        \bool_if:NT \g__cus_cbl_write_bool 
          {
            \iow_new:N \tf@toc 
            \iow_open:Nn \tf@toc 
              { 
                \quark_if_no_value:NTF \g__cus_cbl_to_tl 
                  \c_sys_jobname_str \g__cus_cbl_to_tl .toc 
              }
          }
      }
    \fi:
  }
\int_new:N \g_cus_document_cbl_index_int 
\tl_new:N \g__cus_contents_patch_tl
\tl_gput_right:Nn \g__cus_contents_patch_tl 
  {
    \cs_set_eq:NN \ifintitle \use_ii:nn
    \cs_set_eq:NN \ifincblentry \use_i:nn
    \cs_set_eq:NN \label \__cus_use_none_exp_om:w 
    \cs_set_eq:NN \index \__cus_use_none_exp_om:w 
    \cs_set_eq:NN \glossary \__cus_use_none_exp_om:w 
  }
\cus_pkg_code:nnn { biblatex } { }
  { \tl_gput_right:Nn \g__cus_contents_patch_tl { \blx@contentssafe@citecommands } }
\tl_put_right:Nn \g__cus_cbl_before_load_tl
  {
    \cs_set_eq:NN \addtocontents \cus_gput_contents:nn 
    \cs_set_eq:NN \contentsline \cus_contents_line:nnnnn 
  }
\cs_new_protected:Npn \cus_gput_contents:nn #1#2
  {
    % 为了防止出现 \protect\addvspace{length} 的情况，目录项必须大于 3
    % 如 \protect\contentsline{type}{text}{page}{anchor}，至少有 6 项
    \if_int_compare:w \tl_count:n {#2} > 3 \exp_stop_f:
    \int_gincr:N \g_cus_document_cbl_index_int 
    \protected@write \@auxout
      { \g__cus_contents_patch_tl }
      {
        \token_to_str:N \@writefile { toc } 
          { \__cus_gput_contents:nnn {#1} #2 }
      }
    \else:
      \protected@wlog { Abort~contents~entry:~`#2' }
    \fi:
  }
\cs_new:Npn \__cus_gput_contents:nnn #1#2#3
  { \token_if_eq_meaning:NNTF #2 \protect { #2#3{#1} } { #2{#1}{#3} } }
% 只保存数据，cbl结构为：\.{type}{type count}{info}{level}{entry}{page}{anchor}\@
% 只保存数据，type结构为：\.{type}{cbl count}{info}{level}{entry}{page}{anchor}\@
% type, level, data, page, anchor 
\cs_new_protected:Npn \cus_contents_line:nnnnn #1#2#3#4#5 
  {
    \int_gincr:N \g__cus_cbl_count_int
    \int_gincr:c { g__cus_cbl_type~#1_count_int }
    \clist_set:Nx \l__cus_tmpa_clist {#2}
    \tl_set:Nx \l__cus_cbl_heading_level_tl 
      { \exp_args:Ne \cus_heading_level:n { #1- \l__cus_tmpa_clist } }
    \__cus_cbl_parse_title:n {#3} % save to \l__cus_tmpc_tl
    \tl_gset:cx 
      { cus/cbl~/cbl~ /entry [ \int_use:N \g__cus_cbl_count_int ] _tl }
      {
        \exp_not:N \cus@cbl@contentsline {#1} 
          { \int_use:c { g__cus_cbl_type~#1_count_int } }
          {#2} { \l__cus_cbl_heading_level_tl }
          { \exp_not:o \l__cus_tmpc_tl } {#4} {#5} \exp_not:N \cus@cbl@contentsline@
      }
    \tl_gset:cx 
      { cus/cbl~/type~#1 /entry [ \int_use:c { g__cus_cbl_type~#1_count_int } ] _tl }
      {
        \exp_not:N \cus@type@contentsline {#1}
          { \int_use:N \g__cus_cbl_count_int }
          {#2} { \l__cus_cbl_heading_level_tl }
          { \exp_not:o \l__cus_tmpc_tl } {#4} {#5} \exp_not:N \cus@type@contentsline@
      }
    \tl_gset:cx { cus/cbl~/type~#1 /lo_used_level_tl }
      { 
        \int_max:nn { \l__cus_cbl_heading_level_tl } 
          { \use:c { cus/cbl~/type~#1 /lo_used_level_tl } }
      }
    \tl_gset:cx { cus/cbl~/type~#1 /hi_used_level_tl }
      { 
        \int_min:nn { \l__cus_cbl_heading_level_tl } 
          { \use:c { cus/cbl~/type~#1 /hi_used_level_tl } }
      }
    \if_int_compare:w 
      \int_eval:w \cus_heading_level:n { #1-#2 } = \c_zero_int
      \clist_gput_right:cx { cus/cbl~/type~#1/default~level _count_clist }
        { \int_use:c { g__cus_cbl_type~#1_count_int } }
    \fi:
  }
\cs_new_protected:Npn \__cus_cbl_parse_title:n #1
  {
    \tl_if_head_eq_meaning:nNTF {#1} \numberline
      { \tl_set:No \l__cus_tmpc_tl { \__cus_cbl_parse_title:nnw #1 \s__cus_stop } }
      {
        \tl_if_in:nnTF {#1} { \hspace }
          { \tl_set:Nx \l__cus_tmpc_tl { \__cus_cbl_parse_title:ww #1 \s__cus_stop } }
          { \tl_set:Nn \l__cus_tmpc_tl { \nonumberline{}{#1} } }
      }
  }
\cs_new_protected:Npn \nonumberline #1#2 {#1#2}
\cs_new:Npn \__cus_cbl_parse_title:nnw #1#2#3 \s__cus_stop { #1{#2}{#3} }
\cs_new:Npn \__cus_cbl_parse_title:ww #1 \hspace #2#3 \s__cus_stop 
  { 
    \nonumberline 
      { \exp_not:n {#1} 
        \str_if_eq:nnTF {#2} { * } 
          { \exp_not:N \hspace*{ \tl_head:n {#3} } } 
          { \exp_not:n { \hspace{#2} } }
      }
      { \str_if_eq:nnTF {#2} {*} \tl_tail:n \exp_not:n {#3} } 
  }
\tl_put_right:Nn \g__cus_cbl_after_load_tl
  { % 在这些 clist 后添加一个额外的数字
    \seq_map_inline:Nn \g__cus_cbl_type_seq
      {
        \clist_gput_right:cx { cus/cbl~/type~#1/default~level _count_clist }
          { \int_eval:n { \int_use:c { g__cus_cbl_type~#1_count_int } + 1 } }
      }
  }
\cs_new_protected_nopar:Npn \cus_contents_get:nN #1#2 % cbl count, tl 
  { 
    \tl_if_exist:cTF { cus/cbl~/cbl~ /entry [ \int_eval:n {#1} ] _tl }
      { \tl_set_eq:Nc #2 { cus/cbl~/cbl~ /entry [ \int_eval:n {#1} ] _tl } }
      { \tl_set_eq:NN #2 \q_no_value }
  }
\cs_new_nopar:Npn \cus_contents_entry:n #1 % cbl count 
  { \exp_not:v { cus/cbl~/cbl~ /entry [ \int_eval:n {#1} ] _tl } }
\cs_new_protected_nopar:Npn \contents@entry #1
  { \cs:w cus/cbl~/cbl~ /entry [ \int_eval:n {#1} ] _tl \cs_end: }
\cs_new_nopar:Npn \cus_contents_entry_of:nn #1 % of, count 
  { \cs:w cus_contents_entry_of_ #1 :n \cs_end: }
\cs_new_nopar:Npn \cus_contents_entry_of_type:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_ii:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_nopar:Npn \cus_contents_entry_of_count:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_iii:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_nopar:Npn \cus_contents_entry_of_info:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_iv:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_nopar:Npn \cus_contents_entry_of_level:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_v:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_nopar:Npn \cus_contents_entry_of_entry:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_vi:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_nopar:Npn \cus_contents_entry_of_page:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_vii:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_nopar:Npn \cus_contents_entry_of_anchor:n #1
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_viii:nnnnnnnnn { \cus_contents_entry:n {#1} } 
  }
\cs_new_protected_nopar:Npn \cus_contents_type_get:nnN #1#2#3 % type, type count, tl 
  { 
    \tl_if_exist:cTF { cus/cbl~/type~#1 /entry [ \int_eval:n {#2} ] _tl }
      { \tl_set_eq:Nc #3 { cus/cbl~/type~#1 /entry [ \int_eval:n {#2} ] _tl } }
      { \tl_set_eq:NN #3 \q_no_value }
  }
\cs_new_nopar:Npn \cus_contents_type_entry:nn #1#2 % type, type count 
  { \exp_not:v { cus/cbl~/type~#1 /entry [ \int_eval:n {#2} ] _tl } }
\cs_new_protected_nopar:Npn \contents@typeentry #1#2
  { \cs:w cus/cbl~/type~#1 /entry [ \int_eval:n {#2} ] _tl \cs_end: }
\cs_new_nopar:Npn \cus_contents_type_entry_of:nnn #1#2 % type, of, count 
  { \cs:w cus_contents_type_entry_of_ #2 :nn \cs_end: {#1} }
\cs_new_nopar:Npn \cus_contents_type_entry_of_type:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_ii:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_nopar:Npn \cus_contents_type_entry_of_count:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_iii:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_nopar:Npn \cus_contents_type_entry_of_info:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_iv:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_nopar:Npn \cus_contents_type_entry_of_level:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_v:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_nopar:Npn \cus_contents_type_entry_of_entry:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_vi:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_nopar:Npn \cus_contents_type_entry_of_page:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_vii:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_nopar:Npn \cus_contents_type_entry_of_anchor:nn #1#2
  { 
    \exp_last_unbraced:Ne 
      \cus_exp_not_use_viii:nnnnnnnnn { \cus_contents_type_entry:nn {#1} {#2} } 
  }
\cs_new_protected:Npn \__cus_contentsline_push_def:nnn #1#2#3
  {
    \int_gincr:N \g__cus_map_int
    \tl_if_empty:eTF {#1}
      {
        \cs_gset_eq:cN 
          { __cus_iterate_contents_ \int_use:N \g__cus_map_int :w }
          \cus@cbl@contentsline
        \cs_gset_eq:cN 
          { __cus_iterate_contents@_ \int_use:N \g__cus_map_int :w }
          \cus@cbl@contentsline@
        \cs_set:Npn \cus@cbl@contentsline ##1##2##3##4##5##6##7 {#2}
        \cs_set:Npn \cus@cbl@contentsline@ {#3}
      }
      {
        \cs_gset_eq:cN 
          { __cus_iterate_contents_ \int_use:N \g__cus_map_int :w }
          \cus@type@contentsline
        \cs_gset_eq:cN 
          { __cus_iterate_contents@_ \int_use:N \g__cus_map_int :w }
          \cus@type@contentsline@
        \cs_set:Npn \cus@type@contentsline ##1##2##3##4##5##6##7 {#2}
        \cs_set:Npn \cus@type@contentsline@ {#3}
      }
  }
\cs_new_protected:Npn \__cus_contentsline_pop_def:n #1
  {
    \tl_if_empty:eTF {#1}
      {
        \cs_set_eq:Nc \cus@cbl@contentsline
          { __cus_iterate_contents_ \int_use:N \g__cus_map_int :w }
        \cs_set_eq:Nc \cus@cbl@contentsline@
          { __cus_iterate_contents@_ \int_use:N \g__cus_map_int :w }
      }
      {
        \cs_set_eq:Nc \cus@type@contentsline
          { __cus_iterate_contents_ \int_use:N \g__cus_map_int :w }
        \cs_set_eq:Nc \cus@type@contentsline@
          { __cus_iterate_contents@_ \int_use:N \g__cus_map_int :w }
      }
    \int_gdecr:N \g__cus_map_int
  }
\cs_new_protected:Npn \cus_map_contents_inline:n #1
  {
    \__cus_contentsline_push_def:nnn { } {#1} { }
    \int_step_inline:nn { \g__cus_cbl_count_int }
      { \cs:w cus/cbl~/cbl~ /entry [##1] _tl \cs_end: }
    \__cus_contentsline_pop_def:n { }
  }
\cs_new_protected:Npn \cus_map_contents_type_inline:nn #1#2 % type, code 
  {
    \__cus_contentsline_push_def:nnn {#1} {#2} { }
    \int_step_inline:nn { \int_use:c { g__cus_cbl_type~#1_count_int } }
      { \cs:w cus/cbl~/type~#1 /entry [##1] _tl \cs_end: }
    \__cus_contentsline_pop_def:n {#1}
  }
\cs_new_protected:Npn \__cus_contents_line:nnnnnnn #1#2#3#4#5#6#7
  {
    \cus_use_psr:n { toc / #1 } {#1}{#2}{#3}{#4}{#5}{#6}{#7}
  }
\cs_new_protected:Npn \cus_cbl_fliter:nnnnnnnnn #1#2#3#4#5#6#7 % ,,,,,true,false 
  { \use_i:nn }
\cs_new_protected_nopar:Npn \flitercombinedlistentry { \cus_cbl_fliter:nnnnnnnnn }
\cs_new_protected:Npn \cus_set_filter:n #1 % must end with \if...
  {
    \cs_set_protected:Npn \cus_cbl_fliter:nnnnnnnnn ##1##2##3##4##5##6##7
      {
        #1
          \exp_after:wN \use_i:nn
        \else:
          \exp_after:wN \use_ii:nn
        \fi:
      }
  }
\cs_new_eq:NN \setcombinedlistfilter \cus_set_filter:n 

\cs_new_nopar:Npn \cus_heading_level:n #1
  {
    \cus_if_head_int:nTF {#1}
      { \int_value:w #1 }
      { \__cus_heading_level:w #1 , \s_stop }
  }
\cs_new_nopar:Npn \cus_heading_level:nn #1#2
  { \int_eval:n { \prop_item:Ne \g__cus_heading_level_prop { #1-#2 } + 0 } }
\cs_new_protected_nopar:Npn \cus_get_heading_level:nnN #1#2
  { \exp_args:NNe \prop_get:NnN \g__cus_heading_level_prop { #1-#2 } }
\cs_new:Npn \__cus_heading_level:w #1-#2, #3 \s_stop 
  { 
    \cus_if_head_int:nTF {#2} 
      { \int_eval:w #2 }
      {
        \int_value:w \int_eval:w \prop_item:Ne \g__cus_heading_level_prop
          { #1-\if:w \scan_stop: #2 \scan_stop: main \else: #2 \fi: } + 0 \scan_stop:
      }
  }
\cs_new_protected_nopar:Npn \cus_gset_heading_level:nnn #1#2#3 % type, heading, level 
  {
    \int_gset:Nn \max@headinglevel { \int_min:nn \max@headinglevel { #3 + 0 } }
    \int_gset:Nn \min@headinglevel { \int_max:nn \min@headinglevel { #3 + 0 } }
    \prop_gput:Nxx \g__cus_heading_level_prop { #1-#2 } 
      { \int_eval:n { #3 + 0 } }
  }
\cs_new_protected:Npn \cus_map_heading_level:nn #1#2
  {
    \int_gincr:N \g__cus_map_int
    \cs_set_protected:cpn { __cus_map_tmp_ \int_use:N \g__cus_map_int :w } ##1##2 {#2}
    \tl_if_empty:eTF {#1}
      {
        \exp_args:NNe \prop_map_tokens:Nn \g__cus_heading_level_prop
          {
            \__cus_map_heading_level_aux:Nnn
            \cs:w __cus_map_tmp_ \int_use:N \g__cus_map_int :w \cs_end:
          }
      }
      {
        \exp_args:NNe \prop_map_tokens:Nn \g__cus_heading_level_prop
          {
            \__cus_map_heading_level_aux:Nnnn 
            \cs:w __cus_map_tmp_ \int_use:N \g__cus_map_int :w \cs_end: {#1}
          }
      }
    \int_gdecr:N \g__cus_map_int
  }
\cs_new_protected:Npn \__cus_map_heading_level_aux:Nnn #1#2#3
  { \exp_after:wN #1 \__cus_map_heading_level_aux:w #2 \s__cus_stop }
\cs_new_protected:Npn \__cus_map_heading_level_aux:Nnnn #1#2#3#4
  {
    \str_if_eq:eeT {#2}
      { \exp_after:wN \use_i:nn \__cus_map_heading_level_aux:w #3 \s__cus_stop }
      { \exp_after:wN #1 \__cus_map_heading_level_aux:w #3 \s__cus_stop }
  }
\cs_new:Npn \__cus_map_heading_level_aux:w #1 - #2 \s__cus_stop {{#1}{#2}}
\cs_new_protected_nopar:Npn \cus_cbl_add:nn #1#2 % type , key-val 
  {
    \if_int_compare:w \g__cus_cbl_count_int < \c_one_int
    \int_if_exist:cF { g__cus_cbl_type~#1_count_int }
      { 
        \int_new:c { g__cus_cbl_type~#1_count_int }
        \cs_set_eq:cc { total@type#1@count } { g__cus_cbl_type~#1_count_int }
        \clist_new:c { cus/cbl~/type~#1 /default~level _count_clist }
        \tl_gset:cx { cus/cbl~/type~#1 /hi_used_level_tl } { \int_use:N \c_max_int }
        \tl_gset:cx { cus/cbl~/type~#1 /lo_used_level_tl } { -\int_use:N \c_max_int }
        \seq_put_right:Nx \g__cus_cbl_type_seq {#1}
      }
    \keyval_parse:nnn 
      { \__cus_cbl_add_aux:nn {#1} }
      { \cus_gset_heading_level:nnn {#1} }
      { main, #2 }
    \else:
      \msg_warning:nnn { cus } { struct-unused-cbl } {#1}
    \fi:
  }
\cs_new:Npn \__cus_cbl_add_aux:nn #1#2
  {
    \use:e 
      {
        \cus_gset_heading_level:nnn {#1} 
        \cus_exp_args:Nd \elkernel_exp_not:w { \exp_after:wN \cus_unbracket:nw \elkernel_exp:w { \cus_split_bracket_tail:n {#2} } }
      }
  }
\cs_new_eq:NN \addcombinedlisttype \cus_cbl_add:nn 
\cs_new_nopar:Npn \retcbltypelevel #1#2 { \cus_heading_level:n { #1 - #2 } }
\cs_new:Npn \cus_cbl_get_total_count:n #1
  {
    \tl_if_empty:nTF {#1} { \int_use:N \g__cus_cbl_count_int }
      {
        \int_if_exist:cTF { g__cus_cbl_type~#1_count_int }
          { \int_use:c { g__cus_cbl_type~#1_count_int } }
          { 
            \msg_expandable_error:nnn { cus } { struct-cbl-unknown } {#1}
            \int_value:w \c_zero_int
          }
      }
  }
\cs_new_nopar:Npn \retcbltotalcounts #1 
  { \exp_args:Ne \cus_cbl_get_total_count:n { \tl_trim_spaces:n {#1} } }
\cs_new_nopar:Npn \retcblentryname #1#2
  {
    \tl_if_blank:nTF {#1} 
      { cus/cbl~/cbl~/entry[\int_eval:n{#2}] _tl }
      { cus/cbl~/type~\tl_trim_spaces:n {#1}/entry[\int_eval:n{#2}] _tl }
  }
\cs_new_nopar:Npn \retcbldefaultlevellistname #1
  { cus/cbl~/type~ \tl_trim_spaces:n {#1} /default~level _count_clist }
\cs_new_nopar:Npn \retcblentrydata #1#2
  {
    \tl_if_blank:nTF {#1}
      { \cus_contents_entry_of:nn { \tl_trim_spaces:n {#2} } }
      { \cus_contents_type_entry_of:nnn { \tl_trim_spaces:n {#1} } { \tl_trim_spaces:n {#2} } }
  }
\cs_new_protected_nopar:Npn \iteratecontents #1#2
  {
    \tl_if_blank:nTF {#1}
      { \cus_map_contents_inline:n }
      { \cus_map_contents_type_inline:nn { \tl_trim_spaces:n {#1} } }
  }

\prop_new_linked:N \g__cus_heading_level_prop
\seq_new:N \g__cus_cbl_type_seq 
\int_new:N \g__cus_cbl_count_int
\cs_set_eq:NN \total@cbl@count \g__cus_cbl_count_int
\int_new:N \max@headinglevel % 
\int_new:N \min@headinglevel % min@headinglevel >= max@headinglevel
\int_set:Nn \g__cus_cbl_count_int { -1 }
\cus_new_psr:nnn { toc } { 7 } { }
\cus_new_psr:nnn { toc-level~-1 } { 7 } { }
\cus_new_psr:nnn { toc-level~0 } { 7 } { }
\cus_new_psr:nnn { toc-level~1 } { 7 } { }
\cus_new_psr:nnn { toc-level~2 } { 7 } { }
\cus_new_psr:nnn { toc-level~3 } { 7 } { }
\cus_new_psr:nnn { toc-level~4 } { 7 } { }
\cus_new_psr:nnn { toc-level~5 } { 7 } { }
\cus_new_psrrule:nnn { toc } { level-uniform }
  { \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{#5}{#6}{#7} }
\cus_new_psrrule:nnn { toc } { level-uniform-hyper }
  {
    \str_set:Nx \Hy@tocdestname {#7}
    \if:w \scan_stop: \Hy@tocdestname \scan_stop:
      \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{#5}{#6}{#7}
    \else:
      \if_case:w \Hy@linktoc % none 
        \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{#5}{#6}{#7}
      \or: % section 
        \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{\cus@colorlink\hyper@link{toc}{#7}{#5}}{#6}{#7}
      \or: % page 
        \tl_if_empty:nTF {#6}
          { \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{#5}{#6}{#7} }
          { \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{#5}{\cus@colorlink\hyper@link{toc}{#7}{#6}}{#7} }
      \or:
        \tl_if_empty:nTF {#6}
          { \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{\cus@colorlink\hyper@link{toc}{#7}{#5}}{#6}{#7} }
          { \cus_use_psr:n { toc-level~#4 } {#1}{#2}{#3}{#4}{\cus@colorlink\hyper@link{toc}{#7}{#5}}{\cus@colorlink\hyper@link{toc}{#7}{#6}}{#7} }
      \fi:
    \fi:
  }
\tl_put_right:Nn \g__cus_cbl_before_load_tl
  {
    \seq_map_inline:Nn \g__cus_cbl_type_seq
      {
        \cus_psr_if_exist:nF { toc/#1 }
          {
            \cus_new_psr:nnn { toc/#1 } { 7 }
              { \cus_use_psr:n { toc } {##1}{##2}{##3}{##4}{##5}{##6}{##7} }
          }
      }
  }

\cus_cbl_add:nn { toc } 
  { 
    part[-1], 
    chapter[0], 
    section[1], subsection[2], subsubsection[3], 
    sub3section[4], sub4section[5], 
    paragraph[4], subparagraph[5], 
  }
\cus_cbl_add:nn { lof } { figure=1 }
\cus_cbl_add:nn { lot } { table=1 }
\cus_pkg_code:nnn { algorithm2e } { } { \cus_cbl_add:nn { loa } { algocf=1 } }
\cus_pkg_code:nnn { chemmacros } { } { \cus_cbl_add:nn { lor } { reaction=1 } }
\cus_pkg_code:nnn { hypdvips } { } { \cus_cbl_add:nn { loa } { FileAttachment=1, EmbeddedFile=1 } }
\cus_pkg_code:nnn { musical } { } 
  {
    \cus_cbl_add:nn { los } { section=1 }
    \cus_cbl_add:nn { lod } { section=1 }
  }
\cus_pkg_code:nnn { listings } { } { \cus_cbl_add:nn { lol } { lol=1, lstlisting=1 } }
\cus_pkg_code:nnn { pdfcomment } { } { \cus_cbl_add:nn { lpc } { lpcsec=1 } }
\cus_pkg_code:nnn { poetry } { } { \cus_cbl_add:nn { lop } { poem=1, poemgroup=1 } }
\cus_pkg_code:nnn { todonotes } { } { \cus_cbl_add:nn { tdo } { todo=1 } }
\group_begin:
\char_set_catcode_other:N \#
\use:n
  {
    \group_end:
    \cus_pkg_code:nnn { float } { }
      {
        \ctex_patch_cmd:Nnn \newfloat { \floatplacement }
          { \addcombinedlisttype{#3}{#1=1}\floatplacement }
      }
    \cus_pkg_code:nnn { newfloat } { }
      {
        \ctex_patch_cmd:Nnn \@DeclareFloatingEnvironment 
          { \newfloat@setoptions*{#2}{#1} }
          { \newfloat@setoptions*{#2}{#1}\addcombinedlisttype{\@nameuse{ext@#2}}{#2=1} }
      }
  }
\cus_pkg_code:nnn { floatrow } { }
  {
    \ctex_appto_cmd:NnnTF \DeclareNewFloatType { \ExplSyntaxOff \makeatletter }
      { \addcombinedlisttype{\@nameuse{ext@\FB@captype}}{\FB@captype=1} } { }
      { \cus_patch_failure:N \DeclareNewFloatType }
  }
\cus_pkg_code:nnn { tcolorbox } { }
  {
    \ctex_patch_cmd:Nnn \tcb@proc@options@init
      { \ifx\kvtcb@new@listof\@empty\else }
      { \ifx\kvtcb@new@listof\@empty\else
        \addcombinedlisttype{\kvtcb@new@listof}{\kvtcb@new@listtype=1} }
  }
\cus_pkg_code:nnn { thmtools } { }
  {
    \addcombinedlisttype{loe}{}
    \@ifpackageloaded{amsthm}
      {
        \use:e { \ctex_patch_cmd:Nnn \exp_not:N \@xnthm { \global } 
          { \addcombinedlisttype { \c_hash_str 2 = 1 } \global } }
      } { }
    \def\thmt@mklistcmd{
      \thmtlo@newentry 
      \ifthmt@isstarred 
        \@xa\def\csname ll@\thmt@envname\endcsname{
          \protect\numberline{\relax\protect\let\protect\autodot\protect\@empty}{\thmt@thmname}
          \ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi}
      \else 
        \@xa\def\csname ll@\thmt@envname\endcsname{
          \protect\numberline{\ignorespaces\csname the\thmt@envname\endcsname}
          {\thmt@thmname}
          \ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi}
      \fi
      \@xa\gdef\csname thmt@contentsline@\thmt@envname\endcsname{\thmt@contentslineShow}}
    \renewcommand\listoftheorems[1][]{
      \begingroup \setlisttheoremstyle{#1}
      \let\cus@type@contentsline@\@empty 
      \let\thmt@numberline\numberline 
      \def\cus@type@contentsline##1##2##3##4{
        \exp_args:Ncf\empty{thmt@contentsline@ \cus_clist_item_first:n{##3}}
          {\cus_clist_item_first:n{##3}}}
      \ifthmt@listswap
        \def\numberline##1##2
          {\tl_if_head_eq_meaning:nNTF{##1}\relax{##2}
            {\thmt@numberline{##2\nobreakspace}##1}}
      \else \def\numberline{\thmt@numberline}
      \fi 
      \exp_last_unbraced:NNo \@for\thmt@envname {\string :=}\thmt@allenvs\do{\thmtlo@newentry}
      \multicolplaincombinedlist[1]{\listtheoremname}{loe}
      \endgroup}
    \AtBeginDocument{\def\thmt@contentsline#1#2#3#4
      {\cus_use_psr:n{toc/loe}{loe}{0}{#1}{1}{#2}{#3}{#4}}}
  }

\cs_new_protected_nopar:Npn \cus@cbl@contentsline@ { }
\cs_new_protected_nopar:Npn \cus@type@contentsline@ { }
\cs_new_protected_nopar:Npn \cus@cbl@contentsline { \__cus_contents_line:nnnnnnn }
\cs_new_protected_nopar:Npn \cus@type@contentsline { \__cus_contents_line:nnnnnnn }
\cus_pkg_code:nnn { hyperref }
  { \cus_gbey_psrrule:nn { toc } { level-uniform } }
  {
    \hook_new:n { hyp/link/toc }
    \cus_gbey_psrrule:nn { toc } { level-uniform-hyper }
  }

\if_predicate:w \cs_if_exist_p:N \CTEXnumberline 
\cs_set_eq:NN \CTEX@addloflotskip \use_none:n

\fi:

\msg_new:nnnn { cus } { struct-unused-cbl }
  { The~combinedlist~entry~`#1'~will~never~be~used. }
  { You~must~add~it~before~`\enablecombinedlist'. }
\msg_new:nnn { cus } { struct-cbl-unknown }
  { The~combinedlist~type~`#1'~is~unknown. }

\cs_set_protected:Npn \numberline #1
  {
    \hbox_set:Nn \l__cus_tmpa_box {#1}
    \dim_set:Nn \@tempdima
      {
        \dim_max:nn { \@tempdima }
          { \box_wd:N \l__cus_tmpa_box + \f@size \p@ / 2 }
      }
    \hbox_to_wd:nn \@tempdima { \hbox_unpack_drop:N \l__cus_tmpa_box \tex_hfil:D }
  }


\cs_new_protected:Npn \cus_specified_cbl_style:nnnnnnn #1#2#3#4#5#6#7
% type, level, list start, block start, block entry, block finish, list finish
  {
    \tl_set:cn { @toc@#1@#2@liststart } {#3}
    \tl_set:cn { @toc@#1@#2@blockstart } {#4}
    \tl_set:cn { @toc@#1@#2@blockitem } {#5}
    \tl_set:cn { @toc@#1@#2@blockfinish } {#6}
    \tl_set:cn { @toc@#1@#2@listfinish } {#7}
  }
\cs_new_protected:Npn \cus_specified_cbl_style_save:nnnnnnnN #1#2#3#4#5#6#7#8
% type, level, list start, block start, block entry, block finish, list finish, tl
  {
    % 必须先自行初始化
    \tl_build_put_right:Nx #8
      {
        \tl_set:Nn \exp_not:c { @toc@#1@#2@liststart } { \exp_not:n {#3} }
        \tl_set:Nn \exp_not:c { @toc@#1@#2@blockstart } { \exp_not:n {#4} }
        \tl_set:Nn \exp_not:c { @toc@#1@#2@blockitem } { \exp_not:n {#5} }
        \tl_set:Nn \exp_not:c { @toc@#1@#2@blockfinish } { \exp_not:n {#6} }
        \tl_set:Nn \exp_not:c { @toc@#1@#2@listfinish } { \exp_not:n {#7} }
      }
  }
\cs_new_protected:Npn \cus_specified_cbl:n #1
  {
    \group_begin:
    \tl_if_empty:nTF {#1}
      { 
        \tl_set:Nx \toclolevel { -\int_use:N \c_max_int }
        \tl_set:Nx \tochilevel { \int_use:N \c_max_int }
        \seq_map_inline:Nn \g__cus_cbl_type_seq
          {
            \tl_set:Nx \toclolevel 
              { \int_max:nn { \toclolevel } { \use:c { cus/cbl~/type~##1 /lo_used_level_tl } } }
            \tl_set:Nx \tochilevel 
              { \int_min:nn { \tochilevel } { \use:c { cus/cbl~/type~##1 /hi_used_level_tl } } }
          }
        \cs_set:Npn \__cus_specified_cbl_set_next_entry:
          { \cus_contents_get:nN { \toctheindex+1 } \tocthenextentry }
        \cs_set:Npn \__cus_specified_cbl_set_prev_entry:
          { \cus_contents_get:nN { \toctheindex-1 } \tocthepreventry }
      }
      {
        \tl_set:Nx \toclolevel { \use:c { cus/cbl~/type~#1 /lo_used_level_tl } }
        \tl_set:Nx \tochilevel { \use:c { cus/cbl~/type~#1 /hi_used_level_tl } }
        \cs_set:Npn \__cus_specified_cbl_set_next_entry:
          { \cus_contents_type_get:nnN {#1} { \toctheindex+1 } \tocthenextentry }
        \cs_set:Npn \__cus_specified_cbl_set_prev_entry:
          { \cus_contents_type_get:nnN {#1} { \toctheindex-1 } \tocthepreventry }
      }
    \cus@toc@contentsline@begin 
    \cs_set:Npn \cus@toc@contentsline ##1##2##3##4##5##6##7##8##9
      {
        \tl_gset:Nn \tocthetype {##1}
        \tl_gset:Nn \tocthecount {##2}
        \tl_gset:Nn \toctheindex {##9}
        \__cus_specified_cbl_info:w ##3, \s__cus_mark 
        \tl_gset:Nn \tocthelevel {##4}
        \__cus_specified_cbl_set_prev_entry:
        \tl_gset_eq:NN \tocthepreventry \tocthepreventry 
        \quark_if_no_value:NTF \tocthepreventry 
          { 
            \tl_gset:Nx \toctheprevlevel { - \int_use:N \c_max_int } 
            \cs_gset_eq:NN \tocifheadentry \use_i:nn
          }
          { 
            \tl_gset:Nx \toctheprevlevel { \tl_item:Nn \tocthepreventry { 5 } } 
            \cs_gset_eq:NN \tocifheadentry \use_ii:nn
          }
        \__cus_specified_cbl_set_next_entry:
        \tl_gset_eq:NN \tocthenextentry \tocthenextentry 
        \quark_if_no_value:NTF \tocthenextentry 
          { 
            \tl_gset:Nx \tocthenextlevel { - \int_use:N \c_max_int } 
            \cs_gset_eq:NN \tociftailentry \use_i:nn
          }
          { 
            \tl_gset:Nx \tocthenextlevel { \tl_item:Nn \tocthenextentry { 5 } } 
            \cs_gset_eq:NN \tociftailentry \use_ii:nn
          }
        \tl_gset:No \tocthename { \use_ii:nnn ##5 }
        \tl_gset:No \tocthetitle { \use_iii:nnn ##5 }
        \tl_if_empty:NTF \tocthename
          { \cs_gset_eq:NN \tocifnamed \use_ii:nn }
          { \cs_gset_eq:NN \tocifnamed \use_i:nn }
        \tl_gset:Nn \tocthepage {##6}
        \__cus_if_head_int_dec:nTF { \tocthepage }
          { \cs_gset_eq:NN \tocifpageisnumber \use_i:nn }
          { \cs_gset_eq:NN \tocifpageisnumber \use_ii:nn }
        \tl_gset:Nn \toctheanchor {##7}
        \__cus_specified_cbl_code: 
      }
    \cs_set_eq:NN \tocifheadentry \use_i:nn
    \cs_set_eq:NN \tociftailentry \use_ii:nn
    \cs_set_protected:Npn \toclink { \cus_ref_target:nn \toctheanchor }
    \cs_set_protected:Npn \toclinkbox { \cus_ref_target_box:nn \toctheanchor }
    \cs_set:Npn \cus@cbl@contentsline  { \cus@toc@contentsline }
    \cs_set:Npn \cus@type@contentsline { \cus@toc@contentsline }
    \int_step_inline:nn 
      {
        \tl_if_empty:nTF {#1} { \g__cus_cbl_count_int }
          { \int_use:c { g__cus_cbl_type~#1_count_int } }
      }
      { \cs:w cus/cbl~ \tl_if_empty:nTF {#1} { /cbl~ } { /type~#1 } /entry [##1] _tl \cs_end: {##1} }
    \cus@toc@contentsline@end 
    \group_end:
  }
\cs_new_protected:Npn \cus_specified_cbl:nnn #1#2#3 % toc type, level, curr count
  {
    \group_begin:
    \__cus_specified_cbl_set_special_cs:n {#1}
    \bool_lazy_or:nnTF
      { \tl_if_empty_p:n {#3} }
      { \tl_if_novalue_p:n {#3} }
      { 
        \int_compare:nNnTF { \$ } > { 0 }
          {
            \tl_if_novalue:nTF {#2}
              { \__cus_specified_cbl_local:nnn {#1} {  } { \$ } }
              { \__cus_specified_cbl_local:nnn {#1} {#2} { \$ } }
          }
          { \cus_specified_cbl:n {#1} }
      }
      {
        \int_compare:nNnTF {#3} > { 0 }
          {
            \tl_if_novalue:nTF {#2}
              { \__cus_specified_cbl_local:nnn {#1} {  } {#3} }
              { \__cus_specified_cbl_local:nnn {#1} {#2} {#3} }
          }
          { \cus_specified_cbl:n {#1} }
      }
    \group_end:
  }
\cs_new_protected:Npn \__cus_specified_cbl_local:nnn #1#2#3
  {
    \use:e { \__cus_specified_cbl_local_aux:nnn {#1} {#2} } {#3}
  }
\cs_new_protected:Npn \__cus_specified_cbl_local_aux:nnn #1#2#3
  {
    \tl_if_empty:nTF {#1}
      { 
        \tl_set:Nx \tochilevel { \int_use:N \c_max_int }
        \seq_map_inline:Nn \g__cus_cbl_type_seq
          {
            \tl_set:Nx \tochilevel 
              { \int_min:nn { \tochilevel } { \use:c { cus/cbl~/type~##1 /hi_used_level_tl } } }
          }
        \cs_set:Npn \__cus_specified_cbl_set_next_entry:
          { \cus_contents_get:nN { \toctheindex+1 } \tocthenextentry }
        \cs_set:Npn \__cus_specified_cbl_set_prev_entry:
          { \cus_contents_get:nN { \toctheindex-1 } \tocthepreventry }
      }
      {
        \tl_set:Nx \tochilevel { \use:c { cus/cbl~/type~#1 /hi_used_level_tl } }
        \cs_set:Npn \__cus_specified_cbl_set_next_entry:
          { \cus_contents_type_get:nnN {#1} { \toctheindex+1 } \tocthenextentry }
        \cs_set:Npn \__cus_specified_cbl_set_prev_entry:
          { \cus_contents_type_get:nnN {#1} { \toctheindex-1 } \tocthepreventry }
      }
    \cus_if_head_int:nTF {#2}
      { 
        \int_compare:nNnTF { \tochilevel } > {#2} 
          { \cus_specified_cbl:n {#1} } 
          { 
            \tl_set:Nx \tochilevel { \int_eval:n {#2} }
            \exp_args:Nnf \__cus_specified_cbl_local_real:nnn {#1} { \int_eval:n {#2} } {#3} 
          } 
      }
      { 
        \int_compare:nNnTF { \tochilevel } >
          { \tl_if_empty:nTF {#1} { 0 } { \cus_heading_level:nn {#1} {#2} } }
          { \cus_specified_cbl:n {#1} } 
          {
            \tl_set:Nx \tochilevel 
              { \tl_if_empty:nTF {#1} { 0 } { \cus_heading_level:nn {#1} {#2} } }
            \__cus_specified_cbl_local_real:nnn {#1} {#2} {#3} 
          } 
      }
  }
\cs_new_protected:Npn \__cus_specified_cbl_local_real:nnn #1#2#3
  {
    \tl_set:Nx \toclolevel { -\int_use:N \c_max_int }
    \int_zero:N \l__cus_cbl_search_min_int
    \int_zero:N \l__cus_cbl_search_max_int
    \tl_if_empty:nTF {#1} 
      { 
        \exp_args:Nf \__cus_specified_cbl_search:n 
          { \int_max:nn { 0 } { \int_min:nn { #3 } { \g__cus_cbl_count_int } } }
      }
      { 
        \exp_args:Nnnf \__cus_specified_cbl_search:nnn {#1} {#2}
          {
            \int_max:nn { 0 }
              {
                \int_min:nn { #3 } { \use:c { g__cus_cbl_type~#1_count_int } }
              }
          }
      }
    \cus@toc@contentsline@begin 
    \cs_set:Npn \cus@toc@contentsline ##1##2##3##4##5##6##7##8##9
      {
        \tl_gset:Nn \tocthetype {##1}
        \tl_gset:Nn \tocthecount {##2}
        \tl_gset:Nn \toctheindex {##9}
        \__cus_specified_cbl_info:w ##3, \s__cus_mark 
        \tl_gset:Nn \tocthelevel {##4}
        \__cus_specified_cbl_set_prev_entry:
        \tl_gset_eq:NN \tocthepreventry \tocthepreventry 
        \quark_if_no_value:NTF \tocthepreventry 
          { 
            \tl_gset:Nx \toctheprevlevel { - \int_use:N \c_max_int } 
            \cs_gset_eq:NN \tocifheadentry \use_i:nn
          }
          { 
            \int_compare:nNnTF \toctheindex > \l__cus_cbl_search_min_int
              {
                \tl_gset:Nx \toctheprevlevel { \tl_item:Nn \tocthepreventry { 5 } } 
                \cs_gset_eq:NN \tocifheadentry \use_ii:nn
              }
              {
                \cs_gset_eq:NN \tocthepreventry \q_no_value
                \tl_gset:Nx \toctheprevlevel { - \int_use:N \c_max_int } 
                \cs_gset_eq:NN \tocifheadentry \use_i:nn
              }
          }
        \__cus_specified_cbl_set_next_entry:
        \tl_gset_eq:NN \tocthenextentry \tocthenextentry 
        \quark_if_no_value:NTF \tocthenextentry 
          { 
            \tl_gset:Nx \tocthenextlevel { - \int_use:N \c_max_int } 
            \cs_gset_eq:NN \tociftailentry \use_i:nn
          }
          { 
            \int_compare:nNnTF \toctheindex < \l__cus_cbl_search_max_int
              {
                \tl_gset:Nx \tocthenextlevel { \tl_item:Nn \tocthenextentry { 5 } } 
                \cs_gset_eq:NN \tociftailentry \use_ii:nn
              }
              {
                \cs_gset_eq:NN \tocthenextentry \q_no_value
                \tl_gset:Nx \tocthenextlevel { - \int_use:N \c_max_int } 
                \cs_gset_eq:NN \tociftailentry \use_i:nn
              }
          }
        \tl_gset:No \tocthename { \use_ii:nnn ##5 }
        \tl_gset:No \tocthetitle { \use_iii:nnn ##5 }
        \tl_if_empty:NTF \tocthename 
          { \cs_gset_eq:NN \tocifnamed \use_ii:nn }
          { \cs_gset_eq:NN \tocifnamed \use_i:nn }
        \tl_gset:Nn \tocthepage {##6}
        \__cus_if_head_int_dec:nTF { \tocthepage }
          { \cs_gset_eq:NN \tocifpageisnumber \use_i:nn }
          { \cs_gset_eq:NN \tocifpageisnumber \use_ii:nn }
        \tl_gset:Nn \toctheanchor {##7}
        \__cus_specified_cbl_code: 
      }
    \cs_set_eq:NN \tocifheadentry \use_i:nn
    \cs_set_eq:NN \tociftailentry \use_ii:nn
    \cs_set_protected:Npn \toclink { \cus_ref_target:nn \toctheanchor }
    \cs_set_protected:Npn \toclinkbox { \cus_ref_target_box:nn \toctheanchor }
    \cs_set:Npn \tocretinfo { \prop_item:Nn \toctheprop }
    \cs_set:Npn \cus@cbl@contentsline  { \cus@toc@contentsline }
    \cs_set:Npn \cus@type@contentsline { \cus@toc@contentsline }
    \int_step_inline:nnn \l__cus_cbl_search_min_int \l__cus_cbl_search_max_int
      { \cs:w cus/cbl~ \tl_if_empty:nTF {#1} { /cbl~ } { /type~#1 } /entry [##1] _tl \cs_end: {##1} }
    \cus@toc@contentsline@end 
  }
\cs_new:Npn \__cus_specified_cbl_info:w #1 , #2 \s__cus_mark 
  {
    \tl_gset:Nn \toctheclass {#1}
    \prop_gclear_new:N \toctheinfo 
    \clist_map_inline:nn {#2} { \prop_gput:Nnn \toctheinfo ##1 }
  }
\cs_new:Npn \__cus_specified_cbl_set_special_cs:n #1
  {
    \cs_set_eq:NN \? \g_cus_document_cbl_index_int 
    \bool_lazy_or:nnTF 
      { \tl_if_empty_p:n {#1} } { \int_compare_p:nNn { \? } < { 1 } }
      { \cs_set_eq:NN \$ \? }
      { 
        \bool_set_true:N \l__cus_tmpa_bool 
        \int_set_eq:NN \l__cus_tmpa_int \?
        \bool_while_do:Nn \l__cus_tmpa_bool 
          { 
            \cus_contents_get:nN \l__cus_tmpa_int \l__cus_tmpa_tl 
            \quark_if_no_value:NTF \l__cus_tmpa_tl 
              { 
                \cs_set:Npx \$ { 0 }
                \bool_set_false:N \l__cus_tmpa_bool 
              }
              {
                \str_if_eq:eeTF
                  { \tl_item:Nn \l__cus_tmpa_tl { 2 } } {#1}
                  { 
                    \cs_set:Npx \$ { \tl_item:Nn \l__cus_tmpa_tl { 3 } }
                    \bool_set_false:N \l__cus_tmpa_bool 
                  }
                  {
                    \if_int_compare:w \l__cus_tmpa_int = \c_one_int 
                      \cs_set:Npx \$ { 0 }
                      \bool_set_false:N \l__cus_tmpa_bool 
                    \else:
                      \int_decr:N \l__cus_tmpa_int 
                    \fi:
                  }
              }
          }
      }
  }
\int_new:N \l__cus_cbl_search_min_int
\int_new:N \l__cus_cbl_search_max_int
\cs_new_protected:Npn \__cus_specified_cbl_search:n #1
  { \msg_error:nn {cus} { ? } } %!!TODO:
\cs_new_protected:Npn \__cus_specified_cbl_search_basic:nnn #1#2#3
  {
    \int_set:Nn \l__cus_tmpa_int {#3}
    \int_set:Nn \l__cus_tmpb_int { \cus_heading_level:n { #1-#2 } }
    \bool_set_true:N \l__cus_tmpa_bool
    \cs_set:Npn \cus@type@contentsline ##1##2##3##4##5##6##7##8
      {
        % 如果此时的 level > #2 ，说明在 #2 内部
        \int_compare:nNnTF {##4} > { \l__cus_tmpb_int }
          {
            \int_decr:N \l__cus_tmpa_int
            \if_int_compare:w \l__cus_tmpa_int < \c_one_int
              \int_set:Nn \l__cus_cbl_search_min_int \c_one_int
              \bool_set_false:N \l__cus_tmpa_bool
            \fi:
          }
          {
            \int_compare:nNnTF {##4} = { \l__cus_tmpb_int }
              {
                \__cus_if_head_int_dec:nTF {#2}
                  { \int_set_eq:NN \l__cus_cbl_search_min_int \l__cus_tmpa_int }
                  {
                    \str_if_eq:eeTF { \cus_clist_item_first:n {##3} } {#2}
                      { \int_set_eq:NN \l__cus_cbl_search_min_int \l__cus_tmpa_int }
                      { \int_set:Nn \l__cus_cbl_search_min_int \c_max_int }
                  }
              }
              {
                \int_set:Nn \l__cus_cbl_search_min_int \l__cus_tmpa_int 
              }
            \bool_set_false:N \l__cus_tmpa_bool
          }
      }
    \cus_contents_type_get:nnN {#1} {#3} \l__cus_tmpa_tl 
    \bool_lazy_or:nnTF 
      { \quark_if_no_value_p:N \l__cus_tmpa_tl }
      { \int_compare_p:nNn { \tl_item:Nn \l__cus_tmpa_tl { 5 } } < \l__cus_tmpb_int }
      { \int_set:Nn \l__cus_cbl_search_min_int { \c_max_int } }
      {
        \bool_while_do:Nn \l__cus_tmpa_bool
          { \tl_use:c { cus/cbl~/type~#1 /entry[\int_use:N \l__cus_tmpa_int]_tl } }
      }

    \int_set:Nn \l__cus_tmpa_int { #3+1 }
    \bool_set_true:N \l__cus_tmpa_bool
    \cs_set:Npn \cus@type@contentsline ##1##2##3##4##5##6##7##8
      {
        \int_compare:nNnTF {##4} > { \l__cus_tmpb_int }
          {
            \int_incr:N \l__cus_tmpa_int
            \if_int_compare:w
              \l__cus_tmpa_int > \use:c { g__cus_cbl_type~#1_count_int }
              \bool_set_false:N \l__cus_tmpa_bool
              \int_set:Nn \l__cus_cbl_search_max_int
                { \use:c { g__cus_cbl_type~#1_count_int } }
            \fi:
          }
          {
            \int_set:Nn \l__cus_cbl_search_max_int { \l__cus_tmpa_int - 1 }
            \bool_set_false:N \l__cus_tmpa_bool 
          }
      }
    \int_compare:nNnTF 
      { \l__cus_tmpa_int } > { \use:c { g__cus_cbl_type~#1_count_int } }
      { 
        \int_compare:nNnTF { \l__cus_cbl_search_min_int } > 
          { \use:c { g__cus_cbl_type~#1_count_int } }
          { \int_set:Nn \l__cus_cbl_search_max_int { -\c_max_int } }
          {
            \int_set:Nn \l__cus_cbl_search_max_int 
              { \use:c { g__cus_cbl_type~#1_count_int } } 
          }
      }
      {
        \bool_while_do:Nn \l__cus_tmpa_bool
          { \use:c { cus/cbl~/type~#1 /entry[\int_use:N \l__cus_tmpa_int]_tl } }
      }
    \int_compare:nNnT \l__cus_cbl_search_min_int > {#3}
      { \int_set:Nn \l__cus_cbl_search_max_int { -\c_max_int } }
  }
\cs_new:Npn \__cus_specified_cbl_search:nnn #1#2#3
  {
    \__cus_specified_cbl_search_basic:nnn {#1} {#2} {#3}
    \cs_set:Npn \cus@type@contentsline ##1##2##3##4##5##6##7##8
      { \tl_set:Nx \toclolevel { \int_max:nn { \toclolevel } {##4} } }
    \int_step_inline:nnn \l__cus_cbl_search_min_int \l__cus_cbl_search_max_int
      { \use:c { cus/cbl~/type~#1 /entry[##1]_tl } }
  }
\cs_new:Npn \__cus_specified_cbl_pos:n #1 % pos
  {
    \tl_if_exist:cTF { @toc@\tocthetype @\toctheclass @#1 }
      { \use:c { @toc@\tocthetype @\toctheclass @#1 } }
      { \use:c { @toc@\tocthetype @\tocthelevel @#1 } }
  }
\cs_new:Npn \__cus_specified_cbl_pos:nn #1#2 % level, pos
  {
    \int_compare:nNnTF { \cus_heading_level:nn { \tocthetype } { \toctheclass } }
      = {#1}
      { 
        \tl_if_exist:cTF { @toc@\tocthetype @\toctheclass @#2 }
          { \use:c { @toc@\tocthetype @\toctheclass @#2 } }
          { \use:c { @toc@\tocthetype @#1 @#2 } }
      }
      { \use:c { @toc@\tocthetype @#1 @#2 } }
  }
\cs_new_protected:Npn \__cus_specified_cbl_ifcomplement:TF
  { \exp_last_unbraced:Ne \tl_if_in:nnTF { { -1234567890 } { \tl_head:N \toctheclass } } }
\cs_new_protected:Npn \__cus_specified_cbl_code: 
  {
    \int_compare:nNnTF { \tocthelevel - 1 } > \toctheprevlevel 
      {
        \tociffirsttrue \tocifcomplementtrue 
        \tl_set_eq:NN \cus@toc@tmp \tocthelevel 
        \tocifheadentry 
          { \int_step_inline:nnn { \tochilevel } { \tocthelevel - 1 } }
          { \int_step_inline:nnn { \toctheprevlevel + 1 } { \tocthelevel - 1 } }
            {
              \tl_gset:Nn \tocthelevel {##1}
              \seq_gpush:Nn \toc@classes {##1}
              \__cus_specified_cbl_pos:nn {##1} { liststart }
              \seq_gpush:Nn \toc@classes {##1}
              \__cus_specified_cbl_pos:nn {##1} { blockstart }
              \__cus_specified_cbl_pos:nn {##1} { blockitem }
              \toc@setif {first} {##1} {1}
            }
        \tl_gset_eq:NN \tocthelevel \cus@toc@tmp 
        \tocifcomplementfalse 
        \seq_gpush:No \toc@classes \toctheclass 
        \__cus_specified_cbl_pos:n { liststart }
        \seq_gpush:No \toc@classes \toctheclass 
        \__cus_specified_cbl_pos:n { blockstart }
        \__cus_specified_cbl_pos:n { blockitem }
        \toc@setif {first} {\tocthelevel} {1}
      }
      { 
        \tocifcomplementfalse
        \int_compare:nNnTF { \tocthelevel - 1 } = \toctheprevlevel 
          {
            \tociffirsttrue 
            \seq_gpush:No \toc@classes \toctheclass 
            \__cus_specified_cbl_pos:n { liststart }
            \seq_gpush:No \toc@classes \toctheclass 
            \__cus_specified_cbl_pos:n { blockstart }
            \__cus_specified_cbl_pos:n { blockitem }
            \toc@setif {first} {\tocthelevel} {1}
          }
          {
            \tociffirstfalse 
            \seq_gpush:No \toc@classes \toctheclass 
            \__cus_specified_cbl_pos:n { blockstart } 
            \__cus_specified_cbl_pos:n { blockitem } 
            \toc@setif {first} {\tocthelevel} {0}
          }
      }

    \int_compare:nNnTF { \tocthelevel - 1 } > \tocthenextlevel 
      {
        \tocifcomplementfalse 
        \toc@if {first} {\tocthelevel} { \tociffirsttrue } { \tociffirstfalse }
        \seq_gpop:NN \toc@classes \toctheclass 
        \__cus_specified_cbl_pos:n { blockfinish }
        \tociffirstfalse 
        \seq_gpop:NN \toc@classes \toctheclass 
        \__cus_specified_cbl_ifcomplement:TF 
          { \tocifcomplementtrue } { \tocifcomplementfalse }
        \__cus_specified_cbl_pos:n { listfinish }
        \toc@setif {first} {\tocthelevel} {0}
        \tociftailentry 
          { 
            \int_step_inline:nnnn { \tocthelevel - 1 } { -1 } { \tochilevel }
              {
                \tl_gset:Nn \tocthelevel {##1}
                \toc@if {first} {##1} { \tociffirsttrue } { \tociffirstfalse }
                \seq_gpop:NN \toc@classes \toctheclass 
                \__cus_specified_cbl_ifcomplement:TF 
                  { \tocifcomplementtrue } { \tocifcomplementfalse }
                  \__cus_specified_cbl_pos:nn {##1} { blockfinish }
                \tociffirstfalse 
                \seq_gpop:NN \toc@classes \toctheclass 
                \__cus_specified_cbl_ifcomplement:TF 
                  { \tocifcomplementtrue } { \tocifcomplementfalse }
                \__cus_specified_cbl_pos:nn {##1} { listfinish }
                \toc@setif {first} {##1} {0}
              }
          }
          {
            \int_step_inline:nnnn { \tocthelevel - 1 } { -1 } { \tocthenextlevel + 1 }
              {
                \tl_gset:Nn \tocthelevel {##1}
                \toc@if {first} {##1} { \tociffirsttrue } { \tociffirstfalse }
                \seq_gpop:NN \toc@classes \toctheclass 
                \__cus_specified_cbl_ifcomplement:TF 
                  { \tocifcomplementtrue } { \tocifcomplementfalse }
                \__cus_specified_cbl_pos:nn {##1} { blockfinish }
                \tociffirstfalse 
                \seq_gpop:NN \toc@classes \toctheclass 
                \__cus_specified_cbl_ifcomplement:TF 
                  { \tocifcomplementtrue } { \tocifcomplementfalse }
                \__cus_specified_cbl_pos:nn {##1} { listfinish }
                \toc@setif {first} {##1} {0}
              }
            \tl_gset_eq:NN \tocthelevel \tocthenextlevel
            \toc@if {first} {\tocthelevel} { \tociffirsttrue } { \tociffirstfalse }
            \seq_gpop:NN \toc@classes \toctheclass 
            \tocifcomplementfalse 
            \__cus_specified_cbl_pos:n { blockfinish } 
            \toc@setif {first} {\tocthelevel} {0}
          }
      }
      {
        \int_compare:nNnTF { \tocthelevel - 1 } = { \tocthenextlevel }
          {
            \toc@if {first} {\tocthelevel} { \tociffirsttrue } { \tociffirstfalse }
            \seq_gpop:NN \toc@classes \toctheclass
            \tocifcomplementfalse 
            \__cus_specified_cbl_pos:n { blockfinish }
            \tociffirstfalse 
            \seq_gpop:NN \toc@classes \toctheclass
            \__cus_specified_cbl_ifcomplement:TF 
              { \tocifcomplementtrue } { \tocifcomplementfalse }
            \__cus_specified_cbl_pos:n { listfinish }
            \toc@setif {first} {\tocthelevel} {0}
            \tl_gset_eq:NN \tocthelevel \tocthenextlevel 
            \toc@if {first} {\tocthelevel} { \tociffirsttrue } { \tociffirstfalse }
            \seq_gpop:NN \toc@classes \toctheclass 
            \__cus_specified_cbl_ifcomplement:TF 
              { \tocifcomplementtrue } { \tocifcomplementfalse }
            \__cus_specified_cbl_pos:nn { \tocthenextlevel } { blockfinish }
            \toc@setif {first} {\tocthelevel} {0}
          }
          {
            \int_compare:nNnTF \tocthelevel = \tocthenextlevel 
              {
                \toc@if {first} {\tocthelevel} { \tociffirsttrue } { \tociffirstfalse }
                \seq_gpop:NN \toc@classes \toctheclass 
                \__cus_specified_cbl_pos:n { blockfinish }
                \toc@setif {first} {\tocthelevel} {0}
              }
              { }
          }
      }
  }
\tl_const:Nn \cus@toc@contentsline@begin 
  {
    \cs_set_protected:Npn \tociffirsttrue { \cs_gset_eq:NN \tociffirst \use_i:nn }
    \cs_set_protected:Npn \tociffirstfalse { \cs_gset_eq:NN \tociffirst \use_ii:nn }
    \cs_set_protected:Npn \tocifcomplementtrue { \cs_gset_eq:NN \tocifcomplement \use_i:nn }
    \cs_set_protected:Npn \tocifcomplementfalse { \cs_gset_eq:NN \tocifcomplement \use_ii:nn }
    \tociffirsttrue 
    \int_step_inline:nnn { \tochilevel } { \toclolevel }
      { 
        \bool_set_eq:cN { @toc@complement@ #1 } \c_false_bool 
        \bool_set_eq:cN { @toc@first@ #1 } \c_true_bool
      }
    \cs_set:Npn \toc@if #1#2 { \bool_if:cTF { @toc@#1@#2 } }
    \cs_set_protected:Npn \toc@setif #1#2#3 
      { 
        \int_if_odd:nTF {#3}
          { \bool_gset_true:c { @toc@#1@#2 } }
          { \bool_gset_false:c { @toc@#1@#2 } }
      }
    \seq_clear:N \toc@classes 
  }
\tl_const:Nn \cus@toc@contentsline@end 
  {
    \tl_map_function:nN
      {
        \tocthetype \tocthecount \tocthelevel \tocthepage \toctheanchor 
        \tocthename \tocthetitle \toctheclass 
        \toctheinfo \tocretinfo \toctheindex 
        \tocthepreventry \toctheprevlevel \tocthenextentry \tocthenextlevel 
        \toclink \toclinkbox \tocifcomplement 
        \tociffirst \tocifnamed \tocifpageisnumber \tocifheadentry \tociftailentry 
        \cus@toc@contentsline 
      }
      \cs_undefine:N
  }
\cs_new_protected_nopar:Npn \cus_get_cbl_level_range:nnnNN #1#2#3#4#5
  {
    \group_begin:
    \tl_set:Nx \tochilevel { \use:c { cus/cbl~/type~#1 /hi_used_level_tl } }
    \__cus_specified_cbl_set_special_cs:n {#1}
    \use:e { \__cus_specified_cbl_search_basic:nnn {#1} {#2} } {#3}
    \use:e 
      {
        \group_end:
        \tl_set:Nx \exp_not:N #4 { \int_use:N \l__cus_cbl_search_min_int }
        \tl_set:Nx \exp_not:N #5 { \int_use:N \l__cus_cbl_search_max_int }
      }
  }
\NewDocumentCommand \getcbllevelrange { m m O{} m m }
  {
    \use:e 
      {
        \cus_get_cbl_level_range:nnnNN 
          {#1} {#2} 
          { \tl_if_blank:nTF {#3} { \exp_not:N \$ } { \exp_not:n {#3} } }
      }
          #4 #5
  }

%% specified combinedlist
\NewDocumentCommand \SetSpecifiedCombinedListStyle { O{}+m+m+m+m+m+m }
  {
    \bool_if:NTF \l__cus_specified_cbl_save_bool 
      {
        \cs_set:Npn \__cus_set_specified_combinedlist_style:w ##1##2
          { 
            \cus_specified_cbl_style_save:nnnnnnnN 
              {##1} {##2} {#3} {#4} {#5} {#6} {#7} \g__cus_specified_style_tl 
          }
      }
      {
        \cs_set:Npn \__cus_set_specified_combinedlist_style:w ##1##2
          { 
            \cus_specified_cbl_style:nnnnnnn 
              {##1} {##2} {#3} {#4} {#5} {#6} {#7} 
          }
      }
    \bool_lazy_or:nnTF { \tl_if_empty_p:n {#1} } { \str_if_eq_p:nn {#1} {*} }
      { \seq_map_inline:Nn \g__cus_cbl_type_seq }
      { \clist_map_inline:nn {#1} }
        { 
          \clist_map_inline:nn {#2}
            { \__cus_set_specified_combinedlist_style:w {##1} {####1} }
        }
  }
\bool_new:N \l__cus_specified_cbl_save_bool 
\cs_new_protected:Npn \__cus_saveto_specified_cbl_style:n
  { \tl_build_put_right:Nn \g__cus_specified_style_tl }
\NewDocumentCommand \SaveSpecifiedCombinedListStyle { m +m }
  {
    \@bsphack
    \cs_set_eq:cN { __cus/saved/+ } \+
    \cs_set_eq:NN \+ \__cus_saveto_specified_cbl_style:n
    \bool_set_true:N \l__cus_specified_cbl_save_bool 
    \tl_build_begin:N \g__cus_specified_style_tl 
    #2 
    \tl_build_end:N \g__cus_specified_style_tl
    \tl_set_eq:NN #1 \g__cus_specified_style_tl
    \bool_set_false:N \l__cus_specified_cbl_save_bool 
    \cs_set_eq:Nc \+ { __cus/saved/+ }
    \@esphack 
  }
\NewDocumentCommand \SpecifiedCombinedList { >{\TrimSpaces} O{} }
  { \cus_specified_cbl:n {#1} }
\NewDocumentCommand \LocalSpecifiedCombinedList 
  { >{\TrimSpaces} O{} >{\SplitArgument{1}{,}} D(){,} }
  { \cus_specified_cbl:nnn {#1} #2 }
\cs_new_protected:Npn \tocsetstyle { \SetSpecifiedCombinedListStyle [toc] }
\cs_new_protected:Npn \specifiedtoc { \SpecifiedCombinedList [toc] }
\cs_new_protected:Npn \localspecifiedtoc { \LocalSpecifiedCombinedList [toc] }
\cs_new_protected:Npn \lofsetstyle { \SetSpecifiedCombinedListStyle [lof] {figure} }
\cs_new_protected:Npn \specifiedlof { \SpecifiedCombinedList [lof] }
\cs_new_protected:Npn \lotsetstyle { \SetSpecifiedCombinedListStyle [lot] {table} }
\cs_new_protected:Npn \specifiedlot { \SpecifiedCombinedList [lot] }
\ExplSyntaxOff

%% plain combinedlist
\@namedef{l@toc/cbl/level -1}#1#2#3#4#5#6#7{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \begingroup
      \setlength\@tempdima{3em}%
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large \bfseries {#5}\hfil
       \hb@xt@\@pnumwidth{\hss #6%
                          \kern-\p@\kern\p@}}\par
       \nobreak
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}
\@namedef{l@toc/cbl/level 0}#1#2#3#4#5#6#7{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \begingroup
      \setlength\@tempdima{1.5em}%
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      {#5}\nobreak\hfil
      \nobreak\hb@xt@\@pnumwidth{\hss #6%
                                 \kern-\p@\kern\p@}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else 
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
      \parindent #2\relax\@afterindenttrue
      \interlinepenalty\@M
      \leavevmode
      \@tempdima #3\relax
      \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
      {#4}\nobreak
      \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
      \nobreak\hb@xt@\@pnumwidth{\hfil #5%
                                \kern-\p@\kern\p@}%
      \par}%
  \fi}
\@namedef{l@toc/cbl/level 1}#1#2#3#4#5#6#7{\@dottedtocline{1}{1.5em}{2.3em}{#5}{#6}}
\@namedef{l@toc/cbl/level 2}#1#2#3#4#5#6#7{\@dottedtocline{2}{3.8em}{3.2em}{#5}{#6}}
\@namedef{l@toc/cbl/level 3}#1#2#3#4#5#6#7{\@dottedtocline{3}{7.0em}{4.1em}{#5}{#6}}
\@namedef{l@toc/cbl/level 4}#1#2#3#4#5#6#7{\@dottedtocline{4}{10em}{5em}{#5}{#6}}
\@namedef{l@toc/cbl/level 5}#1#2#3#4#5#6#7{\@dottedtocline{5}{12em}{6em}{#5}{#6}}
\def\l@part#1#2{\@nameuse{l@toc/cbl/level -1}{}{}{}{}{#1}{#2}{}}
\def\l@chapter#1#2{\@nameuse{l@toc/cbl/level 0}{}{}{}{}{#1}{#2}{}}
\def\l@section#1#2{\@nameuse{l@toc/cbl/level 1}{}{}{}{}{#1}{#2}{}}
\def\l@subsection#1#2{\@nameuse{l@toc/cbl/level 2}{}{}{}{}{#1}{#2}{}}
\def\l@subsubsection#1#2{\@nameuse{l@toc/cbl/level 3}{}{}{}{}{#1}{#2}{}}
\def\l@paragraph#1#2{\@nameuse{l@toc/cbl/level 4}{}{}{}{}{#1}{#2}{}}
\def\l@subparagraph#1#2{\@nameuse{l@toc/cbl/level 5}{}{}{}{}{#1}{#2}{}}
\def\l@figure#1#2{\@nameuse{l@toc/cbl/level 0}{}{}{}{}{#1}{#2}{}}
\def\l@table#1#2{\@nameuse{l@toc/cbl/level 0}{}{}{}{}{#1}{#2}{}}


\protected\def\localplaincombinedlist#1#2#3{%
  \begingroup 
  \@tempcnta #2\relax
  \ifnum\@tempcnta<\@ne \@tempcnta\@ne \fi 
  \advance\@tempcnta-\@ne
  \@tempcntb #3\relax 
  \ifnum\@tempcnta>\@tempcntb \@tempcnta\z@ \@tempcntb\maxdimen \fi
  \if\relax#1\relax 
    \ifnum\@tempcntb>\total@cbl@count \@tempcntb\total@cbl@count \fi 
  \else 
    \ifnum\@tempcntb>\@nameuse{total@type#1@count} 
      \@tempcntb\@nameuse{total@type#1@count}
    \fi
  \fi 
  \if\relax#1\relax
    \loop 
      \ifnum\@tempcnta<\@tempcntb
        \advance\@tempcnta by\@ne
        \contents@entry{\the\@tempcnta}\repeat 
  \else 
    \loop 
      \ifnum\@tempcnta<\@tempcntb
        \advance\@tempcnta by\@ne
        \contents@typeentry{#1}{\the\@tempcnta}\repeat 
  \fi
  \endgroup}
\NewDocumentCommand\multicollocalplaincombinedlist
  { O{columns=2,outer-sep=0pt} m m m m } % multicols key, title, type, min, max
  {\if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \protected@edef\@tempc{#2}%
    \if\relax\detokenize\expandafter{\@tempc}\relax\else \chapter{#2}\fi
    \startmulticolumns[#1]%
    \localplaincombinedlist{#3}{#4}{#5}%
    \stopmulticolumns
    \if@restonecol\twocolumn\fi}
\protected\def\plaincombinedlist#1{\localplaincombinedlist{#1}{-\maxdimen}{\maxdimen}}
\NewDocumentCommand\multicolplaincombinedlist{O{columns=2,outer-sep=0pt} m m}
  {\multicollocalplaincombinedlist[{#1}]{#2}{#3}{-\maxdimen}{\maxdimen}}

\let\@starttoc\plaincombinedlist
\DeclareRobustCommand\tableofcontents[1][columns=1]%[columns=2,outer-sep=0pt]
  {\multicolplaincombinedlist[{#1}]{\contentsname}{toc}}
\NewDocumentCommand\localtableofcontents
  { D(){,} O{columns=1} >{\SplitArgument{1}{,}} D(){#1} }
  {\loc@ltableofcontents#3%
    \multicollocalplaincombinedlist[{#2}]{\contentsname}{toc}{\@tempcbla}{\@tempcblb}}
\def\loc@ltableofcontents#1#2{%
  \IfNoValueTF{#1}{\edef\@tempa{\CurrentTitleLevel}}
    {\edef\@tempa{\if\relax#1\relax \CurrentTitleLevel \else #1\fi}}%
  \IfNoValueTF{#2}{\def\@tempb{}}{\def\@tempb{#2}}%
  \expanded{\getcbllevelrange{toc}{\@tempa}[{\@tempb}]}{\@tempcbla}{\@tempcblb}}
\DeclareRobustCommand\listoffigures[1][columns=1]%[columns=2,outer-sep=0pt]
  {\multicolplaincombinedlist[{#1}]{\listfigurename}{lof}}
\DeclareRobustCommand\listoftables[1][columns=1]%[columns=2,outer-sep=0pt]
  {\multicolplaincombinedlist[{#1}]{\listtablename}{lot}}


\ExplSyntaxOn
%% template combinedlist
\ekeysdeclarecmd \templatetoc { &@O{} &@O{} &@D(){} } 
  { \templatecombinedlist#1#2{toc}#3{\contentsname} }
\ekeysdeclarecmd \templatelof { &@O{} &@O{} &@D(){} } 
  { \templatecombinedlist#1#2{lof}#3{\listfigurename} }
\ekeysdeclarecmd \templatelot { &@O{} &@O{} &@D(){} } 
  { \templatecombinedlist#1#2{lot}#3{\listtablename} }
\ekeysdeclarecmd \templatecombinedlist { @O{} @O{} m @D(){} m }
  { \socket_use:nw { templatecbl } {#3} {#1} {#2} {#5} {#4} }
\socket_new:nn { templatecbl } { 5 } % type, tmcbl key, multicols key, title, range
\socket_new_plug:nnn { templatecbl } { by~name }
  {
    \par
    \group_begin:
      \str_set:Nx \tmcblthetype {#1}
      \tmcbl@pushdef \tmcblthetype
        {
          \bgroup \tmcbl@getinfo{##3} 
          \tl_set:Nn \tmcblthelevel {##4}
          \tl_set:Nn \tmcblthepage {##6}
          \str_set:Nn \tmcbltheanchor {##7} 
          \tl_set:No \tmcblthename { \use_ii:nnn ##5 }
          \tl_set:No \tmcblthetitle { \use_iii:nnn ##5 }
          \use:e { \UseInstance { templatecbl } { \tmcbltheclass } } 
            {##1}{##2}{##3}{##4}{##5}{##6}{##7}
        }
        { \egroup }
      \tmcbl@getrange{\tmcblthetype}{#5}
      \keys_set:nn { cus/templatecbl } {#2}
      % multicols key, title, type, min, max
      \multicollocalplaincombinedlist [{columns=1,#3}] {#4} 
        {\tmcblthetype} {\tmcblthemin} {\tmcblthemax}
      \tmcbl@popdef\tmcblthetype
    \group_end:
    \@afterindenttrue
  }
\cs_new_protected:Npn \tmcbl@pushdef { \__cus_contentsline_push_def:nnn }
\cs_new_protected:Npn \tmcbl@popdef { \__cus_contentsline_pop_def:n }
\keys_define:nn { cus/templatecbl }
  {
    no-parent .code:n = \__cus_tmcbl_remove_first_entry: ,
    no-parent .value_forbidden:n = true ,
    hide .code:n = \__cus_tmcbl_set_instances_keys:nn {#1} { ignore=true } ,
    show .code:n = \__cus_tmcbl_set_instances_keys:nn {#1} { ignore=false } ,

    * .code = \__cus_tmcbl_edit_instance:n {#1} ,
    unknown .code:n = 
      \tl_if_head_eq_charcode:oNTF \l_keys_key_str *
        { 
          \exp_last_two_unbraced:Noo \__cus_tmcbl_edit_instance:wn
            \l_keys_key_str { \exp_after:wN { \l_keys_value_tl } }
        }
        {
          \exp_args:Nno \IfInstanceExistsTF { templatecbl } \l_keys_key_str
            { \exp_args:Nooo \EditInstance { templatecbl } \l_keys_key_str \l_keys_value_tl }
            { \msg_error:nnx { cus } { unknown-templatecbl-key } \l_keys_key_str }
        } ,
  }
\cs_new_protected:Npn \tmcbl@getinfo #1 {  \__cus_tmcbl_info:w #1, \s__cus_mark }
\cs_new:Npn \tmcblretinfo { \prop_item:Nn \tmcbltheinfo }
\cs_new:Npn \__cus_tmcbl_info:w #1, #2 \s__cus_mark
  {
    \str_set:Nx \tmcbltheclass {#1}
    \prop_clear_new:N \tmcbltheinfo
    \clist_map_inline:nn {#2} { \prop_put:Nnn \tmcbltheinfo ##1 }
  }
\cs_new_protected:Npn \tmcbl@getrange #1#2
  { \__cus_tmcbl_get_range:wwn #2,,\s__cus_stop {#1} }
\cs_new:Npn \__cus_tmcbl_get_range:wwn #1, #2, #3 \s__cus_stop #4
  {
    \IfBlankTF{#1}{\def\@tempa{\CurrentTitleLevel}}
      {\edef\@tempa{\if\relax#1\relax \CurrentTitleLevel \else #1\fi}}
    \IfBlankTF{#2}{\def\@tempb{}}{\def\@tempb{#2}}%
    \expanded{\ExpandArgs{e}\getcbllevelrange{#4}{\@tempa}[{\@tempb}]}
      {\tmcblthemin}{\tmcblthemax}
  }
\cs_new_protected:Npn \__cus_tmcbl_remove_first_entry:
  {
    \int_compare:nNnT \tmcblthemin < \tmcblthemax
      {
        \int_compare:nNnT \tmcblthemin > { -\maxdimen }
          { \tl_set:Nx \tmcblthemin { \int_eval:n { \tmcblthemin + 1 } } }
      }
  }
\cs_new_protected:Npn \__cus_tmcbl_edit_instance:n #1
  {
    \cus_map_heading_level:nn \tmcblthetype
      {
        \IfInstanceExistsT { templatecbl } {##2}
          { \EditInstance { templatecbl } {##2} {#1} }
      }
  }
\use:e 
  {
    \cs_new_protected:Npn \exp_not:N \__cus_tmcbl_edit_instance:wn 
      * #1 \c_left_brace_str #2 \c_right_brace_str
  }
  { \__cus_tmcbl_set_instances_keys:nn {#2} }
\cs_new_protected:Npn \__cus_tmcbl_set_instances_keys:nn #1#2
  { \clist_map_inline:nn {#1} { \EditInstance { templatecbl } {##1} {#2} } }
\msg_new:nnn { cus } { unknown-templatecbl-key }
  { `#1'~is~neither~a~valid~key~nor~a~instance~for~templatecbl. }
\skip_new:N \tmcbl@beforeskip
\skip_new:N \tmcbl@leftskip
\skip_new:N \tmcbl@rightskip
\dim_new:N \tmcbl@indent
\dim_new:N \tmcbl@hang
\int_new:N \tmcbl@beforepenalty
\tl_new:N \tmcbl@format
\tl_new:N \tmcbl@nameformat
\tl_new:N \tmcbl@titleformat
\tl_new:N \tmcbl@pageformat
\dim_new:N \tmcbl@pagewidth
\dim_new:N \tmcbl@namewidth
\dim_new:N \tmcbl@titlewidth
\dim_new:N \tmcbl@linewidth
\tl_new:N \tmcbl@leadercontent
\bool_new:N \tmcbl@ignore
\cs_new_protected:Npn \tmcbl@code #1#2#3#4#5#6#7 { }
\cs_new_protected:Npn \tmcbl@name { }
\cs_new_protected:Npn \tmcbl@title { }
\cs_new_protected:Npn \tmcbl@page { }
\cs_new_protected:Npn \tmcbl@leader { }
\cs_new_protected:Npn \tmcbl@hyper #1#2#3#4 { }
\int_new:N \tmcbl@hyperrange
\tl_new:N \tmcbl@hyperrangelist
\tl_new:N \tmcbl@before
\tl_new:N \tmcbl@after
\fp_new:N \tmcbl@leadersep
\tl_new:N \tmcbl@leaderoptions
% type, . count, info, level, list entry, thepage, anchor
\cs_new_protected:Npn \tmcbl@code@ #1#2#3#4#5#6#7
  {
    \bool_lazy_and:nnT { !\tmcbl@ignore }
      { \int_compare_p:nNn \c@tocdepth > { #4-1 } }
      {
        { \tmcbl@before
          \mode_if_vertical:T 
            { \unless\ifnum\tmcbl@beforepenalty=\z@\addpenalty{\tmcbl@beforepenalty}\fi
              \tmcbl@skipifnz\vskip\tmcbl@beforeskip }
          \skip_set_eq:NN \leftskip \tmcbl@leftskip
          \skip_set_eq:NN \rightskip \tmcbl@rightskip
          \skip_set:Nn \parfillskip { -\rightskip }
          \dim_set:Nn \parindent \tmcbl@indent
          \mode_if_vertical:T { \interlinepenalty\@M \noindent }
          \dim_set_eq:NN \@tempdima \tmcbl@namewidth % for \numberline
          \null\nobreak \tmcbl@skipifnz\hskip{-\tmcbl@hang}
          { \tmcbl@format \tmcbl@hyper 
            { \tmcbl@name } { \tmcbl@title } { \tmcbl@leader } { \tmcbl@page } }
          \tmcbl@after }
      }
  }
\cs_new_protected:Npn \tmcbl@skipifnz #1#2
  { 
    \str_if_eq:eeF { \skip_eval:n {#2} } { \skip_use:N \c_zero_skip } 
      { #1 \tex_glueexpr:D #2 \scan_stop: }
  }
\cs_new_protected:Npn \tmcbl@name@ 
  { 
    \tl_if_empty:NT \tmcblthename \use_none:n 
      {\numberline{\tmcbl@nameformat\tmcblthename}}
  }
\cs_new_protected:Npn \tmcbl@title@ { {\tmcbl@titleformat\tmcblthetitle} }
\cs_new_protected:Npn \tmcbl@page@
  {
    \hbox_to_wd:nn {\tmcbl@pagewidth} 
      {\hss\tmcbl@pageformat\tmcblthepage\kern-\p@\kern\p@}
  }
\cs_new_protected:Npn \tmcbl@leader@
  {
    \fp_compare:nNnTF { \tmcbl@leadersep } < { 5000 }
      {
        \leaders
          \hbox{$\m@th\mkern\tmcbl@leadersep mu
            \hbox{\tmcbl@leadercontent}\mkern\tmcbl@leadersep mu$}
          \hfill
      }
      { \exp_args:NNo \filler[\tmcbl@leaderoptions] }
  }
\cs_new_protected:Npn \tmcbl@hyperlink { \cus_ref_target:nn {\tmcbltheanchor} }
\cs_new_protected:Npn \tmcbl@hyperlinkbox { \cus_ref_target_box:nn {\tmcbltheanchor} }
\cs_new_protected:Npn \tmcbl@hyper@ #1#2#3#4 % name, title, leader, page
  {
    \int_case:nn { \tmcbl@hyperrange }
      {
        0 { #1#2#3\nobreak#4 }
        1 { \tmcbl@hyperlink{#1}#2#3\nobreak#4 }
        2 { #1\tmcbl@hyperlink{#2}#3\nobreak#4 }
        3 { \tmcbl@hyperlink{#1#2}#3\nobreak#4 }
        4 { #1#2#3\nobreak\tmcbl@hyperlink{#4} }
        5 { \tmcbl@hyperlink{#1}#2#3\nobreak\tmcbl@hyperlink{#4} }
        6 { #1\tmcbl@hyperlink{#2#3\nobreak#4} }
        7 { \tmcbl@hyperlink{#1#2#3\nobreak#4} }
      }
  }
\NewTemplateType { templatecbl } { 7 }
\DeclareTemplateInterface { templatecbl } { by~name } { 7 }
  {
    penalty.before : integer ,

    format         : tokenlist ,
    format.name    : tokenlist ,
    format.title   : tokenlist ,
    format.page    : tokenlist ,

    width.name     : length ,
    width.title    : length ,
    width.page     : length = \@pnumwidth ,
    width.line     : length ,

    space.before   : skip ,
    space.left     : skip ,
    space.right    : skip ,
    space.hang     : length = \KeyValue { width.name } ,
    space.indent   : length = \KeyValue { space.hang } ,

    content.leader : tokenlist = { . } ,

    ignore         : boolean ,

    code           : function{7} = \tmcbl@code@ {#1}{#2}{#3}{#4}{#5}{#6}{#7} ,
    code.name      : tokenlist   = \tmcbl@name@ ,
    code.title     : tokenlist   = \tmcbl@title@ ,
    code.leader    : tokenlist   = \tmcbl@leader@ ,
    code.page      : tokenlist   = \tmcbl@page@ ,
    code.hyper     : tokenlist   = \tmcbl@hyper@ {#1}{#2}{#3}{#4} ,
    code.before    : tokenlist ,
    code.after     : tokenlist   = \par ,

    name.width     : length      = \KeyValue { width.name } ,
    name.format    : tokenlist   = \KeyValue { format.name } ,
    name.code      : tokenlist   = \KeyValue { code.name } ,

    title.width    : length      = \KeyValue { width.title } ,
    title.format   : tokenlist   = \KeyValue { format.title } ,
    title.code     : tokenlist   = \KeyValue { code.title } ,

    page.width     : length      = \KeyValue { width.page } ,
    page.format    : tokenlist   = \KeyValue { format.page } ,
    page.code      : tokenlist   = \KeyValue { code.page } ,

    leader.sep     : real = 4.5 ,
    leader.options : tokenlist   = space ,
    leader.content : tokenlist   = \KeyValue { content.leader } ,
    leader.code    : tokenlist   = \KeyValue { code.leader } ,

    hyper.range    : commalist   = page ,
    hyper.code     : tokenlist   = \KeyValue { code.hyper } ,
  }
\DeclareTemplateCode { templatecbl } { by~name } { 7 }
  {
    penalty.before = \tmcbl@beforepenalty ,

    format         = \tmcbl@format ,
    format.name    = \tmcbl@nameformat ,
    format.title   = \tmcbl@titleformat ,
    format.page    = \tmcbl@pageformat ,

    width.name     = \tmcbl@namewidth ,
    width.title    = \tmcbl@titlewidth ,
    width.page     = \tmcbl@pagewidth ,
    width.line     = \tmcbl@linewidth ,

    space.before   = \tmcbl@beforeskip ,
    space.left     = \tmcbl@leftskip ,
    space.right    = \tmcbl@rightskip ,
    space.hang     = \tmcbl@hang ,
    space.indent   = \tmcbl@indent ,

    content.leader = \tmcbl@leadercontent ,

    ignore         = \tmcbl@ignore ,

    name.width     = \tmcbl@namewidth ,
    name.format    = \tmcbl@nameformat ,
    name.code      = \tmcbl@name ,

    title.width    = \tmcbl@titlewidth ,
    title.format   = \tmcbl@titleformat ,
    title.code     = \tmcbl@title ,

    page.width     = \tmcbl@pagewidth ,
    page.format    = \tmcbl@pageformat ,
    page.code      = \tmcbl@page ,

    leader.sep     = \tmcbl@leadersep ,
    leader.options = \tmcbl@leaderoptions ,
    leader.content = \tmcbl@leadercontent ,
    leader.code    = \tmcbl@leader ,

    hyper.range    = \tmcbl@hyperrangelist ,
    hyper.code     = \tmcbl@hyper ,

    code           = \tmcbl@code ,
    code.name      = \tmcbl@name ,
    code.title     = \tmcbl@title ,
    code.leader    = \tmcbl@leader ,
    code.page      = \tmcbl@page ,
    code.hyper     = \tmcbl@hyper ,
    code.before    = \tmcbl@before ,
    code.after     = \tmcbl@after ,
  }
  {
    \__cus_tmcbl_keys:
    \AssignTemplateKeys
    \__cus_tmcbl_fix_function:
    % type, . count, info, level, list entry, thepage, anchor
    \tmcbl@code {#1}{#2}{#3}{#4}{#5}{#6}{#7}
  }
\cs_new_protected:Npn \__cus_tmcbl_keys: { }
\cs_new:Npn \__cus_tmcbl_hyper_range:n #1
  { \cs_if_exist_use:c { __cus_tmcbl_hyper_range_#1: } }
\cs_new:Npn \__cus_tmcbl_hyper_range_name:  { \int_add:Nn \tmcbl@hyperrange { 1 } }
\cs_new:Npn \__cus_tmcbl_hyper_range_title: { \int_add:Nn \tmcbl@hyperrange { 2 } }
\cs_new:Npn \__cus_tmcbl_hyper_range_page:  { \int_add:Nn \tmcbl@hyperrange { 4 } }
\cs_new:Npn \__cus_tmcbl_hyper_range_none:  { \int_set:Nn \tmcbl@hyperrange { 0 } }
\cs_new:Npn \__cus_tmcbl_hyper_range_all:   { \int_set:Nn \tmcbl@hyperrange { 7 } }
\cs_new_protected:Npn \tmcbl@parsehyperrange #1
  {
    \clist_remove_duplicates:N #1
    \int_zero:N \tmcbl@hyperrange
    \clist_map_function:NN #1 \__cus_tmcbl_hyper_range:n
  }
\cs_new_protected:Npn \__cus_tmcbl_fix_function:
  {
    \tmcbl@parsehyperrange \tmcbl@hyperrangelist
    \tl_set:Nx \tmcbl@leadersep { \fp_to_decimal:N \tmcbl@leadersep }
    \exp_args:Nno \use:n { \cs_set_protected:Npn \tmcbl@hyper ##1##2##3##4 }
      { \tmcbl@hyper }
  }
\ExplSyntaxOff
\AssignSocketPlug{templatecbl}{by name}
\DeclareInstance{templatecbl}{part}{by name}{
  space.before = 2.25em plus 1pt,
  space.right  = \@pnumwidth,
  space.hang   = 0pt,
  format       = \large\bfseries,
  width.name   = 3em,
  width.page   = 0em,
  leader.sep   = 5000, % >=5000, no leader
  penalty.before = -\@highpenalty,
}
\DeclareInstance{templatecbl}{chapter}{by name}{
  space.before = 1em plus 1pt,
  space.right  = \@pnumwidth,
  space.hang   = 0pt,
  format       = \bfseries,
  width.name   = 1.5em,
  % width.page   = \@pnumwidth, % default=\@pnumwidth
  leader.sep   = 5000,
  penalty.before = -\@highpenalty,
}
\DeclareInstance{templatecbl}{section}{by name}{
  space.before = 0pt plus .2pt,
  space.left   = 2.3em+1.5em,
  space.right  = \@tocrmarg,
  space.hang   = 2.3em,
  width.name   = 2.3em,
}
\DeclareInstance{templatecbl}{subsection}{by name}{
  space.before = 0pt plus .2pt,
  space.left   = 3.2em+3.8em,
  space.right  = \@tocrmarg,
  space.hang   = 3.2em,
  width.name   = 3.2em,
}
\DeclareInstance{templatecbl}{subsubsection}{by name}{
  space.before = 0pt plus .2pt,
  space.left   = 4.1em+7em,
  space.right  = \@tocrmarg,
  space.hang   = 4.1em,
  width.name   = 4.1em,
}
\DeclareInstance{templatecbl}{paragraph}{by name}{
  space.before = 0pt plus .2pt,
  space.left   = 5em+10em,
  space.right  = \@tocrmarg,
  space.hang   = 5em,
  width.name   = 5em,
}
\DeclareInstance{templatecbl}{subparagraph}{by name}{
  space.before = 0pt plus .2pt,
  space.left   = 6em+12em,
  space.right  = \@tocrmarg,
  space.hang   = 6em,
  width.name   = 6em,
}
\DeclareInstanceCopy{templatecbl}{figure}{chapter}
\DeclareInstanceCopy{templatecbl}{table}{chapter}


%% modify placeins.sty
\def\@fb@botlist{\@botlist}
\def\@fb@topbarrier{\suppressfloats[t]}
\protected\def\AllowFloatBelowHeading{\def\@fb@botlist{}}
\protected\def\AllowFloatAboveHeading{\def\@fb@topbarrier{}}
\protected\def\FloatBarrier{\par\begingroup \let\@elt\relax
  \edef\@tempa{\@fb@botlist\@deferlist\@dbldeferlist}%
  \ifx\@tempa\@empty 
  \else
    \ifx\@fltovf\relax % my indicator of recursion
      \if@firstcolumn 
        \clearpage 
      \else 
        \null\newpage\FloatBarrier 
      \fi
    \else 
      \newpage \let\@fltovf\relax 
      \FloatBarrier % recurse once only
  \fi\fi \endgroup
  \@fb@topbarrier}
%%

\ExplSyntaxOn

\cus_new_psrrule_eq_cs:nnc { toc-level~-1 } { standard } { l@toc/cbl/level~-1 }
\cus_new_psrrule_eq_cs:nnc { toc-level~0 } { standard } { l@toc/cbl/level~0 }
\cus_new_psrrule_eq_cs:nnc { toc-level~1 } { standard } { l@toc/cbl/level~1 }
\cus_new_psrrule_eq_cs:nnc { toc-level~2 } { standard } { l@toc/cbl/level~2 }
\cus_new_psrrule_eq_cs:nnc { toc-level~3 } { standard } { l@toc/cbl/level~3 }
\cus_new_psrrule_eq_cs:nnc { toc-level~4 } { standard } { l@toc/cbl/level~4 }
\cus_new_psrrule_eq_cs:nnc { toc-level~5 } { standard } { l@toc/cbl/level~5 }
\cus_obey_psrrule:nn { toc-level~-1 } { standard }
\cus_obey_psrrule:nn { toc-level~0 } { standard }
\cus_obey_psrrule:nn { toc-level~1 } { standard }
\cus_obey_psrrule:nn { toc-level~2 } { standard }
\cus_obey_psrrule:nn { toc-level~3 } { standard }
\cus_obey_psrrule:nn { toc-level~4 } { standard }
\cus_obey_psrrule:nn { toc-level~5 } { standard }

\cs_new_protected:Npn \SetPlainTocLevelCode #1 #2
  {
    \cus_set_psrrule:nnn { toc-level~ \cus_heading_level:n { toc-#1 } } 
      { standard }
      {#2}
    \cus_obey_psrrule:nn { toc-level~ \cus_heading_level:n { toc-#1 } } { standard }
  }


\msg_new:nnn { cus } { title-class-unknown } { The~title~class~`#1'~unknown. }
\prop_new_linked:N \g__cus_title_href_prop 
\cs_new:Npn \custitleanchor 
  { \cus_exp_args:NNe \prop_item:Nn \g__cus_title_href_prop }
\int_new:N \g_cus_title_int
\seq_new:N \g__cus_title_seq
\cs_new_eq:NN \ifintitle \use_ii:nn
\cs_new_eq:NN \ifincblentry \use_ii:nn
\prop_new:N \g__cus_title_info_prop
\prop_new:N \g__cus_title_info_stay_prop
\ekeysdeclarecmd \titleaddinfo { s m }
  { 
    \protected@edef \@tempa {#2}
    \bool_if:nTF {#1} 
      { \prop_gput:Non \g__cus_title_info_stay_prop }
      { \prop_gput:Non \g__cus_title_info_prop }
        \@tempa 
  }
\ekeysdeclarecmd \titleremoveinfo { s m }
  {
    \protected@edef \@tempa {#2}
    \bool_if:nTF {#1}
      { \prop_gremove:NV \g__cus_title_info_stay_prop }
      { \prop_gremove:NV \g__cus_title_info_prop }
        \@tempa 
  }
\ekeysdeclarecmd \titleremoveallinfo { s }
  {
    \bool_if:nTF {#1}
      { \prop_gclear:N \g__cus_title_info_stay_prop }
      { \prop_gclear:N \g__cus_title_info_prop }
  }
\cs_new_protected:Npn \IterateTitleList { \seq_map_inline:Nn \g__cus_title_seq }
\NewDocumentCommand \definetitle { m >{ \TrimSpaces } O{normal} +m } 
  { 
    \cs_if_exist:cF { title@class@ #2 }
      { \msg_error:nnn { cus } { title-class-unknown } {#2} }
    \exp_args:NNe \seq_if_in:NnF \g__cus_title_seq { \cs_to_str:N #1 }
      { \exp_args:Ne \__cus_define_title_initial:n { \cs_to_str:N #1 } }
    \exp_args:Nee \keys_define:nn { cus/title/ \cs_to_str:N #1 } 
      { \exp_not:v { title@classkeys@#2 } }
    \str_gset:cx { title~parameter@ \cs_to_str:N #1 @__class } {#2}
    \exp_args:Nee \cus_setup_title:nn { \cs_to_str:N #1 }
      { \exp_not:v { title@classinitial@ #2 } , \exp_not:n {#3} }
    \use:c { title@classhook@#2afterdef }
    \use:e 
      {
        \__cus_title_declare_cmd:NnN 
          \exp_not:N #1 { \cs_to_str:N #1 } { \exp_not:c { title@class@ #2 } }
      }
  }
% title cmd, title name, class cmd 
\cs_new_protected:Npn \__cus_title_declare_cmd:NnN #1#2#3
  {
    \DeclareDocumentCommand #1 { s t- +O{} O{} m }
      {
        \__cus_title_initial:n {#2}
        \bool_if:NT ##2
          {
            \cus_setup_title:nn {#2} { mode=nonumber }
            \__cus_store_title_setting_key:nnn {#2} { mode } { }
          }
        \tl_if_blank:nTF {##4}
          {
            \cus_if_keyval_variable:nNnn {##3}
              \l__cus_title_curr_keys_tl
              {
                \__cus_title:onnnnn \l__cus_title_curr_keys_tl
                  {#2} {#3} {##1} {##5} {##5}
              }
              {
                \tl_if_empty:NTF \l__cus_title_curr_keys_tl
                  { \__cus_title:nnnnnn { } {#2} {#3} {##1} {##5} {##5} }
                  { \__cus_title:nnnnnn { } {#2} {#3} {##1} {##3} {##5} }
              }
          }
          { \__cus_title:nnnnnn {##3} {#2} {#3} {##1} {##4} {##5} }
        \__cus_restore_title_setting:n {#2}
        \__cus_title_ending:
      }
  }
\tl_new:N \g__cus_title_next_keys_tl
\bool_new:N \g__cus_title_next_known_bool
% key, title str, class, star or else, entry, title 
\cs_new_protected:Npn \__cus_title:nnnnnn #1#2 
  {
    \exp_args:Nno \__cus_store_title_setting:nn 
      {#2} { \g__cus_title_next_keys_tl , #1 }
    \tl_if_blank:oTF \g__cus_title_next_keys_tl
      { \cus_setup_title:nn {#2} {#1} }
      {
        \bool_if:NTF \g__cus_title_next_known_bool
          {
            \exp_args:Nno \cus_setup_known_title:nn 
              {#2} { \g__cus_title_next_keys_tl , #1 }
          }
          {
            \exp_args:Nno \cus_setup_title:nn 
              {#2} { \g__cus_title_next_keys_tl , #1 }
          }
      }
    \tl_gclear:N \g__cus_title_next_keys_tl
    \bool_gset_false:N \g__cus_title_next_known_bool
    \__cus_title:nnnnn {#2}
  }
\cs_new_protected:Npn \__cus_title:onnnnn { \exp_args:No \__cus_title:nnnnnn }
% title str, class, star or else, entry, title 
\cs_new_protected:Npn \__cus_title:nnnnn #1#2#3#4#5
  {
    \use:e 
      { 
        \exp_not:n {#2} 
        {#1} {#3} 
        { \tl_if_blank:nTF {#4} { \exp_not:n {#5} } { \exp_not:n {#4} } } 
      } 
        {#5}
  }
\ekeysdeclarecmd \setupnexttitle { t? t+ m }
  {
    \bool_gset:Nn \g__cus_title_next_known_bool {#1}
    \bool_if:nTF {#2} \tl_gput_right:Nn \tl_gset:Nn \g__cus_title_next_keys_tl {,#3}
  }
\ekeysdeclarecmd \setuptitle { @O{*} m }
  {
    \seq_set_from_clist:Nn \l__cus_tmpa_seq {#1}
    \seq_if_in:NnTF \l__cus_tmpa_seq { * }
      { \seq_map_inline:Nn \g__cus_title_seq }
      { \seq_map_inline:Nn \l__cus_tmpa_seq }
        { \setuponetitle {##1} {#2} }
  }
\cs_new_protected:Npn \cus_setup_title:nn #1 { \keys_set:nn { cus/title/#1 } }
\clist_new:N \l__cus_title_unknown_keys_clist
\cs_new_protected:Npn \cus_setup_known_title:nn #1 
  { \keys_set_known:nnN { cus/title/#1 } \l__cus_title_unknown_keys_clist }
\cs_new_protected:Npn \cus_title_level_do:nn #1#2
  {
    \cs_set:Npn \__cus_title_level_do:n ##1 {#2}
    \seq_map_inline:Nn \g__cus_title_seq
      {
        \int_compare:nNnT {#1} = { \use:c { title~parameter@ ##1 @level } }
          { \__cus_title_level_do:n {##1} }
      }
  }
\cs_new_protected_nopar:Npn \setuponetitleq #1 { \cus_setup_title:nn {#1} }
\cs_new_protected:Npn \setuponetitle #1#2
  {
    \cus_if_head_int:nTF {#1}
      { 
        \exp_args:Nf \cus_title_level_do:nn { \int_eval:n {#1} } 
          { \cus_setup_title:nn {##1} {#2} }
      }
      { 
        \cs_if_exist:cTF {#1}
          { \cus_setup_title:nn {#1} {#2} }
          { \msg_warning:nnx { cus } { unknown-title } { \c_backslash_str #1 } }
      }
  }
\msg_new:nnn { cus } { unknown-title } { Unknown~title~command:~#1. }

\cs_new_protected:Npn \__cus_define_title_initial:n #1
  {
    \int_if_exist:cTF { c@ #1 } 
      {
        \exp_args:Nc \e@alloc@chardef { title~parameter@#1@__counter_number }
          \cus_get_register_num:cn { c@ #1 } { int } \exp_stop_f:
      }
      { 
        \newcounter {#1} 
        \exp_args:Nc \e@alloc@chardef { title~parameter@#1@__counter_number } \allocationnumber 
      }
    \int_zero:c { c@ #1 }
    \seq_put_right:Nx \g__cus_title_seq { #1 } 
    % 需要定义 toclevel@#1 , 用于 bookmark
    \tl_set:cx { toclevel@ #1 } { \exp_not:c { title~parameter@ #1 @level } }
    \exp_args:Ne \__cus_define_title_keys:n { #1 }
    \clist_new:c { title~parameter@#1@info }
    \tl_new:c { title~parameter@pre #1 }
    \tl_new:c { title~parameter@post #1 }
    \tl_const:cx { titlethe #1 }
      {
        \exp_not:c { title~parameter@pre #1 }
        \exp_not:c { title~parameter@the #1 }
        \exp_not:c { title~parameter@post #1 }
      }
    \tl_const:cx { title~parameter@ #1 name }
      {
        \group_begin:
          \exp_not:c { title~parameter@#1@nameformat }
            {
              \exp_not:c { title~parameter@pre #1 }
              \exp_not:N \tl_if_empty:NTF
              \exp_not:c { title~parameter@ #1 @numberformat }
                { \exp_not:c { title~parameter@the #1 } }
                {
                  \group_begin:
                    \exp_not:c { title~parameter@ #1 @numberformat }
                    \exp_not:c { title~parameter@the #1 }
                  \group_end:
                }
              \exp_not:c { title~parameter@post #1 }
            }
        \group_end:
      }
  }
\keys_define:nn { cus } { title .meta:nn = { cus/title } {#1} }
\cs_new_nopar:Npn \titleinternalvalue #1 { \cs:w title~parameter@#1 \cs_end: }
\cs_new_protected:Npn \__cus_define_title_keys:n #1
  {
    % 这些键都使用预定义的通用恢复方法（所有标题都一致的恢复方法）
    % 区别于默认恢复方法，它们可能无需恢复，也可能需要特殊处理
    \cus_setup_title_store_restore:nnn {#1}
      { 
        style, name, name-preto, name-appto, number, numbering, 
        number-from, number-within, number-without, 
        number-format, label-format,
        beforerecord<, beforerecord>, properties+, 
        bookmark, bookmark*,
        break+, format+, nameformat+, numberformat+, titleformat+, 
        aftername+, aftertitle+, 
      }
      { general }
    \keys_define:nn { cus/title } { #1 .meta:nn = { cus/title/#1 } {##1} }
    \keys_define:nn { cus/title/ #1 }
      {
        style           .meta:n = {##1} ,
        name            .code:n = { \cus_assign_heading_name:nn {#1} {##1} } ,
        name-preto      .code:n = 
          { \tl_put_left:cn { title~parameter@pre #1 } {##1} } ,
        name-appto      .code:n = 
          { \tl_put_right:cn { title~parameter@post #1 } {##1} } ,
        number        .tl_set:c = title~parameter@the #1 ,
        number-from     .code:n = 
          \__cus_title_keys_preamble_set:nn { number-from }
            { \cus_assign_number_from:nn {#1} {##1} } ,
        number-within   .code:n = 
          \__cus_title_keys_preamble_set:nn { number-within }
            { \counterwithin {#1} {##1} \scan_stop: } ,
        number-without  .code:n = 
          \__cus_title_keys_preamble_set:nn { number-without }
            { \counterwithout {#1} {##1} \scan_stop: } ,
        number-format   .code:n = 
          \__cus_title_keys_preamble_set:nn { number-format }
            { \cs_set:cpn { the #1 } {##1} } ,
        label-format    .code:n = 
          \__cus_title_keys_preamble_set:nn { label-format }
            { \labelformat {#1} { ##1 } } ,
        beforeskip   .tl_set:c = title~parameter@#1@beforeskip ,
        afterskip    .tl_set:c = title~parameter@#1@afterskip ,
        leftskip     .tl_set:c = title~parameter@#1@leftskip ,
        rightskip    .tl_set:c = title~parameter@#1@rightskip ,
        indent       .tl_set:c = title~parameter@#1@indent ,
        numbering     .choice: ,
        numbering / true .code:n = \__cus_title_set_mode:nn {#1} { normal } ,
        numbering / yes  .code:n = \__cus_title_set_mode:nn {#1} { normal } ,
        numbering / on  .code:n = \__cus_title_set_mode:nn {#1} { normal } ,
        numbering / false.code:n = \__cus_title_set_mode:nn {#1} { nonumber } ,
        numbering / no   .code:n = \__cus_title_set_mode:nn {#1} { nonumber } ,
        numbering / off  .code:n = \__cus_title_set_mode:nn {#1} { nonumber } ,
        numbering     .default:n = true ,
        mode            .code:n = \__cus_title_set_mode:nn {#1} {##1} ,
        mode         .initial:n = normal ,
        beforeskip   .initial:n = \c_zero_skip ,
        afterskip    .initial:n = \c_zero_skip ,
        leftskip     .initial:n = \c_zero_skip ,
        rightskip    .initial:n = \c_zero_skip ,
        indent       .initial:n = \c_zero_dim ,
        afterindent .switch_set:c = title~parameter@#1@afterindent ,
        fixskip     .switch_set:c = title~parameter@#1@fixskip ,
        ensureskip  .switch_set:c = title~parameter@#1@ensureskip ,
        hang        .switch_set:c = title~parameter@#1@hang ,
        hang           .initial:n = true ,
        runin       .switch_set:c = title~parameter@#1@runin ,
        float-barrier .switch_set:c = title~parameter@#1@floatbarrier ,
        pagestyle   .tl_set_e:c = title~parameter@#1@pagestyle ,
        pagestyle    .initial:n = plain ,
        mark          .tl_set:c = title~parameter@#1@mark ,
        mark         .initial:n = \use_none:n ,
        level           .code:n = 
          \__cus_title_keys_preamble_set:nn { title~level }
            { \cus_assign_heading_level:nn {#1} {##1} } ,
        beforerecord  .tl_set:c = { title~parameter@#1@beforerecord } ,
        beforerecord>   .code:n = \tl_put_left:cn 
          { title~parameter@#1@beforerecord } {##1} ,
        beforerecord<   .code:n = \tl_put_right:cn 
          { title~parameter@#1@beforerecord } {##1} ,
        tocline      .cs_set:cp = { title~parameter@#1@tocline } ##1##2 ,
        tocline      .initial:n = \titlenumberline{##1}##2 ,
        properties .clist_set:c = { title~parameter@#1@properties } ,
        properties+ .code:n = \clist_put_right:cn 
          { title~parameter@#1@properties } {##1} ,
        bookmark*       .code:n = \cus_gset_next_bookmark_text:n {##1} ,
        bookmark        .code:n = 
          \cus_gset_next_bookmark_text:n 
            { 
              \legacy_if:nT { BKM@numbered } 
                { \titleifnamed { \tl_use:c { titlethe #1 } ~ } { } } ##1 
            } ,
        bookmark-extra.tl_set:c = { title~parameter@#1@bookmark-extra } ,
        break         .tl_set:c = title~parameter@#1@break ,
        break+          .code:n = \tl_put_right:cn { title~parameter@#1@break } {##1} ,
        format        .tl_set:c = title~parameter@#1@format ,
        format+         .code:n = \tl_put_right:cn { title~parameter@#1@format } {##1} ,
        nameformat    .tl_set:c = title~parameter@#1@nameformat ,
        nameformat+     .code:n = \tl_put_right:cn { title~parameter@#1@nameformat } {##1} ,
        numberformat  .tl_set:c = title~parameter@#1@numberformat ,
        numberformat+   .code:n = \tl_put_right:cn { title~parameter@#1@numberformat } {##1} ,
        titleformat   .tl_set:c = title~parameter@#1@titleformat ,
        titleformat+    .code:n = \tl_put_right:cn { title~parameter@#1@titleformat } {##1} ,
        aftername     .tl_set:c = title~parameter@#1@aftername ,
        aftername+      .code:n = \tl_put_right:cn { title~parameter@#1@aftername } {##1} ,
        aftertitle    .tl_set:c = title~parameter@#1@aftertitle ,
        aftertitle+     .code:n = \tl_put_right:cn { title~parameter@#1@aftertitle } {##1} ,
      }
  }
\seq_new:N \l__cus_title_restore_key_seq
\cs_new_protected:Npn \__cus_title_initial:n #1
  { 
    \int_gincr:N \g_cus_title_int
    \tl_gset:Nx \CurrentTitleLevel { \tl_use:c { title~parameter@ #1 @level } }
    \tl_gset:Nn \CurrentTitleName  {#1}
    \cs_set_eq:NN \ifintitle \use_i:nn
  }
\tl_const:Nn \thetitlecount { \int_use:N \g_cus_title_int }
\def\@titlecount@last{-\maxdimen} \def\PreviousTitleCount{0}
\cus_hook_gput_ae:n 
  { \ifnum\@titlecount@last>\z@ \xdef\PreviousTitleCount{\@titlecount@last}\fi }
\tl_gput_left:Nn \@kernel@after@enddocument@afterlastpage
  { \iow_now:Ne \@auxout { \gdef\string\@titlecount@last{\thetitlecount} } }
\tl_gset:Nx \CurrentTitleLevel { -10001 }
\str_new:N \CurrentTitleName

% 在设置标题选项时，需要保存此前的值，在标题完成之后，再恢复至原来的值
% 这通过 \__cus_store_title_setting:nn 和 \__cus_restore_title_setting:n 完成
% 如果需要特殊的处理，可以使用
% \cus_set_title_key_store_code:nn 和 \cus_set_title_key_restore_code:nn 
\cs_new_protected:Npn \__cus_store_title_setting:nn #1#2
  {
    \keyval_parse:nnn
      { \cus_twiddle_N:nnn { \__cus_store_title_setting_key:nnn {#1} } { } }
      { \__cus_store_title_setting_key:nnn {#1} }
      {#2}
    \use:c { title~restore@#1@extra } % extra store code 
  }
\cs_new_protected:Npn \__cus_store_title_setting_key:nnn #1#2#3
  {
    \cs:w title~store@#1@#2@command \cs_end: % 可以通过它来改变 store 方法
    \__cus_store_title_setting_key:nn {#1} {#2}
  }
\cs_new_protected:Npn \__cus_store_title_setting_key:nn #1#2
  {
    % 如果没有该变量，且没有设置特殊的处理方式，那就不恢复
    \cs_if_exist:cT { title~parameter@#1@#2 }
      { 
        \seq_put_right:Nn \l__cus_title_restore_key_seq {#2}
        \cs_set_eq:cc { title~tmpstore@#1@#2 } { title~parameter@#1@#2 } 
      }
  }
\cs_new_protected:Npn \cus_set_title_key_store_code:nn #1
  { 
    \cs_set_protected:cpn { title~store@ #1 @command } 
      \__cus_store_title_setting_key:nn ##1##2
  }
\cs_new_protected_nopar:Npn \__cus_restore_title_setting:n #1
  {
    \seq_remove_duplicates:N \l__cus_title_restore_key_seq
    \seq_map_inline:Nn \l__cus_title_restore_key_seq
      {
        \cs:w title~restore@#1@##1@command \cs_end: % 可以通过它来改变 restore 方法
        \__cus_restore_title_setting_key:nn {#1} {##1}
      }
    \seq_clear:N \l__cus_title_restore_key_seq
  }
\cs_new_protected:Npn \__cus_restore_title_setting_key:nn #1#2
  { \cs_set_eq:cc { title~parameter@#1@#2 } { title~tmpstore@#1@#2 } }
\cs_new_protected:Npn \cus_set_title_key_restore_code:nn #1
  {
    \cs_set_protected:cpn { title~restore@ #1 @command }
      \__cus_restore_title_setting_key:nn ##1##2
  }
\cs_new_protected:Npn \__cus_title_ending:
  {
    \prop_gclear:N \g__cus_title_info_prop
    \clist_gclear:c { title~parameter@ \CurrentTitleName @info }
    \cs_set_eq:NN \ifintitle \use_ii:nn
  }

% 设置标题#1的恢复方法
\cs_new_protected:Npn \cus_setup_title_store_restore:nn #1
  { 
    \keyval_parse:nnn 
      { \__cus_setup_title_store_restore:nn {#1} } % 如果没有值，则表示忽略该键
      { \__cus_setup_title_store_restore:nnn {#1} } 
  }
% 设置标题#1的键#2使用预定义的恢复方法#3
% 预定义的方法有：none（忽略）、general（通用的恢复方法）、default（默认的恢复方法）
\cs_new_protected:Npn \cus_setup_title_store_restore:nnn #1#2#3
  {
    \clist_map_inline:nn {#2}
      { \__cus_setup_title_store_restore_by:nnn {#1} {##1} {#3} }
  }
% 设置标题通用的恢复方法
\cs_new:Npn \cus_setup_title_store_restore:n { \cus_setup_title_store_restore:nn { } }
\cs_new:Npn \__cus_setup_title_store_restore:nn #1#2 % title,key
  { \__cus_setup_title_store_restore:nnnn {#1} {#2} { } { } }
\cs_new:Npn \__cus_setup_title_store_restore:nnn #1#2#3 % title,key,store restore code
  {
    % \bool_lazy_and:nnTF 
    %   { \tl_if_head_is_group_p:n {#3} }
    %   { \int_compare_p:nNn { 2 } = { \tl_count:n {#3} } }
    \tl_if_head_is_group:nTF {#3}
      { \__cus_setup_title_store_restore:nnnn {#1} {#2} #3 } % 直接设置为它们
      { \__cus_setup_title_store_restore_by:nnn {#1} {#2} {#3} }
  }
\cs_new_protected:Npn \__cus_setup_title_store_restore:nnnn #1#2#3#4
  {
    \cus_set_title_key_store_code:nn   { #1@#2 } {#3}
    \cus_set_title_key_restore_code:nn { #1@#2 } {#4}
  }
\cs_new_protected:Npn \__cus_setup_title_store_restore_by:nnn #1#2#3
  { \use:c { __cus_setup_title_store_restore_#3:nn } {#1} {#2} }
\cs_new_protected:Npn \__cus_setup_title_store_restore_:nn #1#2 % none
  { \__cus_setup_title_store_restore:nnnn {#1} {#2} { } { } }
\cs_new_eq:NN 
  \__cus_setup_title_store_restore_none:nn
  \__cus_setup_title_store_restore_:nn
\cs_new_protected:Npn \__cus_setup_title_store_restore_general:nn #1#2
  {
    \cs_set_eq:cc { title~store@#1@#2@command } { title~store@@#2@command }
    \cs_set_eq:cc { title~restore@#1@#2@command } { title~restore@@#2@command }
  }
\cs_new_protected:Npn \__cus_setup_title_store_restore_default:nn #1#2
  {
    \cs_set_eq:cN { title~store@#1@#2@command } \scan_stop:
    \cs_set_eq:cN { title~restore@#1@#2@command } \scan_stop:
  }
\cus_setup_title_store_restore:n 
  {
    style ,
    number-from ,
    number-within ,
    number-without ,
    number-format ,
    label-format ,
    bookmark ,
    bookmark* ,
    bookmark-extra ,
    % 以上这些键，不需要保存和恢复

    , name=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { name }
        \cs_set_eq:cc { title~tmpstore@pre #1 } { title~parameter@pre #1 }
        \cs_set_eq:cc { title~tmpstore@post #1 } { title~parameter@post #1 }
      }
      {
        \cs_set_eq:cc { title~parameter@pre #1 } { title~tmpstore@pre #1 }
        \cs_set_eq:cc { title~parameter@post #1 } { title~tmpstore@post #1 }
      }
    , name-preto=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { name-preto }
        \cs_set_eq:cc { title~tmpstore@pre #1 } { title~parameter@pre #1 }
      }
      { \cs_set_eq:cc { title~parameter@pre #1 } { title~tmpstore@pre #1 } }
    , name-appto=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { name-appto }
        \cs_set_eq:cc { title~tmpstore@post #1 } { title~parameter@post #1 }
      }
      { \cs_set_eq:cc { title~parameter@post #1 } { title~tmpstore@post #1 } }
    , number=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { number }
        \cs_set_eq:cc { title~tmpstore@the #1 } { title~parameter@the #1 }
      }
      { \cs_set_eq:cc { title~parameter@the #1 } { title~tmpstore@the #1 } }
    , numbering=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { mode }
        \cs_set_eq:cc { title~tmpstore@#1@mode } { title~parameter@#1@mode }
      }
      { \cs_set_eq:cc { title~parameter@#1@mode } { title~tmpstore@#1@mode } }
    , beforerecord<=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { beforerecord }
        \cs_set_eq:cc { title~tmpstore@#1@beforerecord } { title~parameter@#1@beforerecord }
      }
      { \cs_set_eq:cc { title~parameter@#1@beforerecord } { title~tmpstore@#1@beforerecord } }
    , beforerecord>=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { beforerecord }
        \cs_set_eq:cc { title~tmpstore@#1@beforerecord } { title~parameter@#1@beforerecord }
      }
      { \cs_set_eq:cc { title~parameter@#1@beforerecord } { title~tmpstore@#1@beforerecord } }
    , properties+=
    {
      \seq_put_right:Nn \l__cus_title_restore_key_seq { properties }
      \cs_set_eq:cc { title~tmpstore@#1@properties } { title~parameter@#1@properties }
    }
    { \cs_set_eq:cc { title~parameter@#1@properties } { title~tmpstore@#1@properties } }
    , break+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { break }
        \cs_set_eq:cc { title~tmpstore@#1@break } { title~parameter@#1@break }
      }
      { \cs_set_eq:cc { title~parameter@#1@break } { title~tmpstore@#1@break } }
    , format+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { format }
        \cs_set_eq:cc { title~tmpstore@#1@format } { title~parameter@#1@format }
      }
      { \cs_set_eq:cc { title~parameter@#1@format } { title~tmpstore@#1@format } }
    , nameformat+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { nameformat }
        \cs_set_eq:cc { title~tmpstore@#1@nameformat } { title~parameter@#1@nameformat }
      }
      { \cs_set_eq:cc { title~parameter@#1@nameformat } { title~tmpstore@#1@nameformat } }
    , numberformat+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { numberformat }
        \cs_set_eq:cc { title~tmpstore@#1@numberformat } { title~parameter@#1@numberformat }
      }
      { \cs_set_eq:cc { title~parameter@#1@numberformat } { title~tmpstore@#1@numberformat } }
    , titleformat+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { titleformat }
        \cs_set_eq:cc { title~tmpstore@#1@titleformat } { title~parameter@#1@titleformat }
      }
      { \cs_set_eq:cc { title~parameter@#1@titleformat } { title~tmpstore@#1@titleformat } }
    , aftername+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { aftername }
        \cs_set_eq:cc { title~tmpstore@#1@aftername } { title~parameter@#1@aftername }
      }
      { \cs_set_eq:cc { title~parameter@#1@aftername } { title~tmpstore@#1@aftername } }
    , aftertitle+=
      {
        \seq_put_right:Nn \l__cus_title_restore_key_seq { aftertitle }
        \cs_set_eq:cc { title~tmpstore@#1@aftertitle } { title~parameter@#1@aftertitle }
      }
      { \cs_set_eq:cc { title~parameter@#1@aftertitle } { title~tmpstore@#1@aftertitle } }
    , 
  }

\cs_new_protected:Npn \__cus_title_keys_preamble_set:nn #1 % warning words, set code 
  {
    \cus_if_document_test:TF 
      { \msg_warning:nnn { cus } { document-reset-value } { #1 }  }
  }
\cs_new_protected:Npn \__cus_title_info_gput:nn #1#2
  { \clist_gput_right:cn { title~parameter@ \CurrentTitleName @info } { {#1}{#2} } }
% unstarred mode is odd 
\int_const:cn { title~constant@normal~mode } \c__cus_one_int
\int_const:cn { title~constant@unstarred~mode } \c__cus_one_int
\int_const:cn { title~constant@nonumber~mode } \c__cus_three_int
% starred mode is even 
\int_const:cn { title~constant@starred~mode } \c__cus_two_int
\cs_new_protected:Npn \__cus_title_set_mode:nn #1#2
  {
    \cs_if_exist:cTF { title~constant@#2~mode }
      { \cs_set_eq:cc { title~parameter@#1@mode } { title~constant@#2~mode } }
      { \msg_error:nnxx { cus } { title-unknown-mode } { \c_backslash_str #1 } {#2} }
  }
\msg_new:nnn { cus } { title-unknown-mode } 
  { Unknown~title~mode~`#2'~when~setting~#1. }
\cs_new:Npn \__cus_title_if_mode:nnTF #1#2 
  {
    \exp_after:wN \if_meaning:w \cs:w title~parameter@#1@mode \exp_after:wN \cs_end:
      \cs:w title~constant@#2~mode \cs_end:
      \exp_after:wN \use_i:nn
    \else: \exp_after:wN \use_ii:nn
    \fi:
  }
% see \str_case:nnTF, etc.
\cs_new:Npn \__cus_title_mode_case:nnTF #1#2#3#4
  {
    \exp:w \__cus_title_mode_case:nw {#1} #2 {#1} { }
      \s__cus_mark {#3} \s__cus_mark {#4} \s__cus_stop
  }
\cs_new:Npn \__cus_title_mode_case:nw #1#2#3
  {
    \__cus_title_if_mode:nnTF {#1} {#2}
      { \__cus_title_mode_case_end:nw {#3} }
      { \__cus_title_mode_case:nw {#1} }
  }
\cs_new:Npn \__cus_title_mode_case_end:nw #1#2#3 \s__cus_mark #4#5 \s__cus_stop
  { \exp_end: #1 #4 }
\NewDocumentCommand \cus_assign_heading_name:nn
  { m > { \SplitArgument { 1 } { , } } +m }
  { \__cus_assign_heading_name:nnn {#1} #2 }
\cs_new_protected:Npn \__cus_assign_heading_name:nnn #1#2#3
  {
    \tl_set:cn { title~parameter@pre #1 } {#2}
    \tl_if_novalue:nTF {#3}
      { \tl_clear:c { title~parameter@post #1 } }
      { \tl_set:cn { title~parameter@post #1 } {#3} }
  }
\cs_new_protected:Npn \cus_assign_heading_level:nn #1#2
  { \tl_set:cx { title~parameter@ #1 @level } { \cus_heading_level:n { toc-#2 } } }
\cs_new_protected:Npn \cus_assign_number_from:nn #1#2
  {
    \str_if_eq:eeTF {#1} {#2}
      {
        \exp_args:Ncc \tex_countdef:D { c@ #2 } { title~parameter@#1@__counter_number }
      }
      { 
        % 先前已经分配了 \c@#1 ，这里并不取消它
        \int_if_exist:cTF { c@ #2 } { \cs_set_eq:cc { c@ #1 } { c@ #2 } }
          { \msg_error:nnn { cus } { counter-unknown } {#2} }
      }
  }
\NewDocumentCommand \setsecnumberdepth { >{\TrimSpaces} m }
  { 
    \cus_if_head_int:nTF {#1} { \setcounter { secnumdepth } { \int_eval:n {#1} } }
      { \exp_args:Nnf \setcounter { secnumdepth } { \cus_heading_level:n { toc-#1 } } }
  }
\cs_new_protected_nopar:Npn \setsecnumdepth { \setsecnumberdepth }
\NewDocumentCommand \settocdepth { >{\TrimSpaces} m }
  { 
    \cus_if_head_int:nTF {#1} { \setcounter { tocdepth } { \int_eval:n {#1} } }
      { \exp_args:Nnf \setcounter { tocdepth } { \cus_heading_level:n { toc-#1 } } }
  }
\cs_new_protected:Npn \setplaintocdepth { \settocdepth }

\msg_new:nnn { cus } { document-reset-value } { Cannot~set~#1~after~preamble. }

\cs_new_eq:NN \CurrentCombinedListCount \g_cus_document_cbl_index_int 
\int_new:N \g__cus_curr_toc_default_level_count_int
\cs_new_eq:NN \CurrentTocDefaultLevelCount \g__cus_curr_toc_default_level_count_int
\cs_new_protected:Npn \cus@updatedefaultlevel #1#2
  {
    \str_if_eq:eeT {#1} { toc }
      { 
        \if_int_compare:w \exp_args:Ne \cus_heading_level:n { toc-#2 } = \c_zero_int
          \int_gincr:N \g__cus_curr_toc_default_level_count_int
        \fi:
      }
  }
\group_begin:
\char_set_catcode_other:N \#
\cus_hook_gput_ae:n 
  {
    \ctex_appto_cmd:NnnTF \addcontentsline { \makeatletter }
      { \cus@updatedefaultlevel{#1}{#2} }
      { }
      { \cus_patch_failure:N \addcontentsline }
  }
\group_end:
\skip_new:N \title@headingskip
\cs_new:Npn \title@mark #1 
  { \cs:w title~parameter@ \CurrentTitleName @mark \cs_end: {#1} }
\cs_new:Npn \title@floatbarrier #1 { }
\cs_new:Npn \title@break
  { \cs:w title~parameter@ \CurrentTitleName @break \cs_end: }
\cs_new:Npn \title@pagestyle
  { 
    \tl_if_empty:cF { title~parameter@ \CurrentTitleName @pagestyle } 
      { \exp_args:Nv \usethispagestyle { title~parameter@ \CurrentTitleName @pagestyle } }
  }
\cs_new:Npn \title@setheadingskip #1
  { \skip_set:Nn \title@headingskip { \tl_use:c { title~parameter@ \CurrentTitleName @#1skip } } }
\cs_new:Npn \title@iffixskip
  { \bool_if:cTF { title~parameter@ \CurrentTitleName @fixskip } }
\cs_new:Npn \title@ifensureskip
  { \bool_if:cTF { title~parameter@ \CurrentTitleName @ensureskip } }
\cs_new:Npn \title@fixheadingskip
  {
    \par 
    \dim_set:Nn \tex_prevdepth:D { -1000pt }
    \skip_sub:Nn \title@headingskip { \tex_parskip:D }
  }
\cs_new:Npn \title@fixtopskip 
  {
    \title@fixheadingskip 
    \dim_compare:nNnF \tex_pagegoal:D < \c_max_dim
      { \skip_sub:Nn \title@headingskip { \tex_topskip:D } }
  }
\cs_new:Npn \title@ifnumbering #1#2
  { \title@ifmode { \CurrentTitleName } { nonumber } {#2} {#1} }
\cs_new:Npn \title@ifmode { \__cus_title_if_mode:nnTF }
\cs_new:Npn \title@modecase { \__cus_title_mode_case:nnTF }
\cs_new_protected:Npn \title@makeanchor { \cus_make_unique_target:n }
\cs_new:Npn \title@gettitle #1 { }
\cus_pkg_code:nnn { nameref } { }
  { \cs_gset_protected:Npn \title@gettitle { \NR@gettitle } }
\cs_new:Npn \title@beforerecord
  { 
    \use:c { title~parameter@ \CurrentTitleName @beforerecord } 
    \prop_gput:Nxx \g__cus_title_href_prop 
      { \int_use:N \g_cus_title_int } { \@currentHref }
  }
\cs_new:Npn \title@recordproperties
  {
    \property_record:ee 
      { cus.title. \int_use:N \g_cus_title_int }
      { \exp_not:v { title~parameter@ \CurrentTitleName @properties } }
  }
\cs_new:Npn \__cus_if_incsname:TF 
  { \tex_ifincsname:D \exp_after:wN \use_i:nn \else: \exp_after:wN \use_ii:nn \fi: }
\cs_new:Npn \title@extras { \title@bookmarkextra \title@recordproperties }
\cs_new:Npn \title@bookmarkextra
  { 
    \tl_if_empty:cF { title~parameter@ \CurrentTitleName @bookmark-extra }
      {
        \exp_last_unbraced:Ne \cus_bookmark:nn 
          { 
            \exp_args:Nv \__cus_title_bookmark:n 
              { title~parameter@ \CurrentTitleName @bookmark-extra } 
          }
        \tl_clear:c { title~parameter@ \CurrentTitleName @bookmark-extra }
      }
  }
\cs_new:Npn \__cus_title_bookmark:n #1
  { \exp_last_unbraced:Ne \__cus_title_bookmark:wn { \cus_split_bracket_head:n {#1} } }
\cs_new:Npn \__cus_title_bookmark:wn [#1] #2 { {#1} {#2} }
\cs_new:Npn \title@addtocline #1
  {
    \prop_gconcat:NNN \g__cus_title_info_prop
      \g__cus_title_info_stay_prop \g__cus_title_info_prop
    \prop_map_function:NN \g__cus_title_info_prop \__cus_title_info_gput:nn
    \addcontentsline {toc} 
      % bookmark needs #1 to determine level
      { 
        \CurrentTitleName \__cus_if_incsname:TF { } 
          { , \use:c { title~parameter@ \CurrentTitleName @info } } 
      } 
      { 
        \cs:w title~parameter@ \CurrentTitleName @tocline \exp_after:wN \cs_end: 
          \exp_after:wN { \CurrentTitleName } {#1} 
      } 
  }
\cs_new:Npn \title@heading@format@initial 
  {
    \normalfont
    \int_set:Nn \tex_interlinepenalty:D { 10000 }
    \tex_noindent:D 
  }
\cs_new:Npn \titleifnamed #1#2 {#2}
\cs_new:Npn \title@ifnametrue { \cs_set_eq:NN \titleifnamed \use_i:nn }
\cs_new:Npn \title@ifnamefalse { \cs_set_eq:NN \titleifnamed \use_ii:nn }
\cs_new:Npn \titleifmeasuring #1#2 {#2}
\cs_new:Npn \title@ifmeasuringtrue { \cs_set_eq:NN \titleifmeasuring \use_i:nn }
\cs_new:Npn \title@ifmeasuringfalse { \cs_set_eq:NN \titleifmeasuring \use_ii:nn }
\cs_new:Npn \title@format { \cs:w title~parameter@ \CurrentTitleName @format \cs_end: }
\cs_new:Npn \title@headinghang #1
  {
    \dim_set:Nn \tex_parindent:D { \title@getindent }
    \bool_if:cTF { title~parameter@ \CurrentTitleName @hang }
      {
        \tex_noindent:D 
        \hbox_set:Nn \l__cus_tmpa_box
          {
            \dim_compare:nNnF \tex_parindent:D = \c_zero_dim
              { \tex_indent:D }
            #1
          }
        \tex_hangindent:D = \box_wd:N \l__cus_tmpa_box
        \box_use_drop:N \l__cus_tmpa_box
      }
      {
        \dim_compare:nNnF \tex_parindent:D = \c_zero_dim
          { \tex_indent:D }
        #1
      }
  }
\cs_new:Npn \title@titleformat 
  { \cs:w title~parameter@ \CurrentTitleName @titleformat \cs_end: }
\cs_new:Npn \title@aftertitle 
  { \cs:w title~parameter@ \CurrentTitleName @aftertitle \cs_end: }
\cs_new:Npn \title@ifafterindent
  { \bool_if:cTF { title~parameter@ \CurrentTitleName @afterindent } }
\cs_new:Npn \title@name { \cs:w title~parameter@ \CurrentTitleName name \cs_end: }
\cs_new:Npn \title@aftername 
  { \cs:w title~parameter@ \CurrentTitleName @aftername \cs_end: }
\cs_new:Npn \title@getlevel
  { \cs:w title~parameter@ \CurrentTitleName @level \cs_end: }
\cs_new:Npn \title@ifrunin 
  { \bool_if:cTF { title~parameter@ \CurrentTitleName @runin } }
\cs_new:Npn \title@iffloatbarrier
  { \bool_if:cTF { title~parameter@ \CurrentTitleName @floatbarrier } }
\cs_new:Npn \title@indentbox #1
  {
    \dim_set:Nn \tex_parindent:D {#1}
    \dim_compare:nNnF \tex_parindent:D = \c_zero_dim { \tex_indent:D }
  }
\cs_new:Npn \title@getindent 
  { \cs:w title~parameter@ \CurrentTitleName @indent \cs_end: }

\cus_add_pdf_disable:n
  {
    \cs_set_eq:NN \par \c_space_tl
    \cs_set_eq:NN \\ \c_space_tl
  }
\cs_new_protected:Npn \title@setvarwidth #1#2#3
  { \collectn_set_vbox_varwidth:Nnw #1 {#2} #3 \collectn_set_vbox_varwidth_end: }
\ExplSyntaxOff

\let\hyper@nopatch@sectioning\@empty 
\let\NR@nopatch@sectioning\@empty

\def\title@ifstar#1{\ifnum
  \ifx\BooleanTrue#1\@empty 1\else 0\fi
  \title@ifmode\CurrentTitleName{starred}10=0
  \expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi}

\newbox\title@box 
\newdimen\titlewidth 

% 例如：\part
\def\title@classkeys@page{}
\def\title@classinitial@page{pagestyle=plain,level=part,hang=false,}
\def\title@classhook@pagebegin{}
\def\title@classhook@pageend{}
\def\title@classhook@pageafterdef{}
\protected\def\title@class@page #1#2{%
  \title@break
  \title@classhook@pagebegin
  \title@pagestyle
  \if@twocolumn 
    \onecolumn
    \@tempswatrue
  \else 
    \@tempswafalse
  \fi 
  \title@setheadingskip{before}%
  \title@iffixskip{\title@fixtopskip}{}%
  \vspace*{\title@headingskip}%
  \title@ifstar{#2}{\title@class@page@@}{\title@class@page@}{#1}}
\protected\def\title@class@page@ #1#2#3{%
  \ifnum \c@secnumdepth >-2\relax
    \title@ifnumbering
      {\title@ifnametrue \refstepcounter{#1}}
      {\title@ifnamefalse \title@makeanchor{#1}}%
  \else 
    \title@ifnamefalse
    \title@makeanchor{#1}%
  \fi 
  \title@beforerecord
  \title@gettitle{#2}%
  \title@addtocline{#2}%
  \title@extras
  \title@mark{#2}%
  \begingroup
    \title@heading@format@initial 
    \title@format{%
      \title@headinghang
        {\titleifnamed{\title@name\title@aftername}{}}%
      \title@titleformat{#3}%
      \title@aftertitle}\par 
  \endgroup
  \title@class@pageend{#1}}
\protected\def\title@class@page@@ #1#2#3{%
  \title@ifnamefalse 
  \title@makeanchor{#1}%
  \title@beforerecord
  \title@gettitle{#2}%
  \title@extras
  \begingroup
    \title@heading@format@initial 
    \title@format{%
      \title@headinghang{}%
      \title@titleformat{#3}%
      \title@aftertitle}\par 
  \endgroup
  \title@class@pageend{#1}}
\protected\def\title@class@pageend #1{%
  \title@setheadingskip{after}%
  \title@iffixskip{\title@fixheadingskip}{}%
  \vskip\title@headingskip
  \title@classhook@pageend
  \newpage 
  \if@twoside
    \if@openright \null \thispagestyle{empty}\newpage \fi
  \fi 
  \if@tempswa \twocolumn \fi}
% 例如：\chapter 
\def\title@classkeys@top{}
\def\title@classinitial@top{pagestyle=plain,level=chapter,hang=false}
\def\title@classhook@topbegin{}
\def\title@classhook@topend{}
\def\title@classhook@topafterdef{}
\protected\def\title@class@top #1#2{%
  \title@break
  \title@classhook@topbegin
  \title@pagestyle
  \global\@topnum\z@ 
  \title@ifafterindent{\@afterindenttrue}{\@afterindentfalse}%
  \title@ifstar{#2}{\title@class@top@@}{\title@class@top@}{#1}}
\protected\def\title@class@top@ #1#2#3{%
  \ifnum \c@secnumdepth >\m@ne
    \title@ifnumbering
      {\title@ifnametrue \refstepcounter{#1}}
      {\title@ifnamefalse \title@makeanchor{#1}}%
  \else 
    \title@ifnamefalse 
    \title@makeanchor{#1}%
  \fi 
  \title@beforerecord
  \title@gettitle{#2}%
  \title@addtocline{#2}%
  \title@extras
  \title@mark{#2}%
  \if@twocolumn 
    \@topnewpage[\@maketophead{#1}{#3}]%
  \else 
    \@maketophead{#1}{#3}%
    \@afterheading
  \fi}
\protected\def\title@class@top@@ #1#2#3{%
  \title@ifnamefalse 
  \title@makeanchor{#1}%
  \title@beforerecord
  \title@gettitle{#2}%
  \title@extras
  \if@twocolumn 
    \@topnewpage[\@makestophead{#1}{#3}]%
  \else 
    \@makestophead{#1}{#3}%
    \@afterheading
  \fi}
\protected\def\@maketophead #1#2{%
  \title@setheadingskip{before}%
  \title@iffixskip{\title@fixtopskip}{}%
  \vspace*{\title@headingskip}%
  \begingroup
    \title@heading@format@initial 
    \title@format{%
      \title@headinghang{\titleifnamed{\title@name\title@aftername}{}}%
      \title@titleformat{#2}%
      \title@aftertitle}\par 
  \endgroup
  \nobreak 
  \title@setheadingskip{after}%
  \title@iffixskip{\title@fixheadingskip}{}%
  \vskip\title@headingskip
  \title@classhook@topend}
\protected\def\@makestophead #1#2{%
  \title@setheadingskip{before}%
  \title@iffixskip{\title@fixtopskip}{}%
  \vspace*{\title@headingskip}%
  \begingroup
    \title@heading@format@initial 
    \title@format{%
      \title@headinghang{}%
      \title@titleformat{#2}%
      \title@aftertitle}\par 
  \endgroup
  \nobreak 
  \title@setheadingskip{after}%
  \title@iffixskip{\title@fixheadingskip}{}%
  \vskip\title@headingskip
  \title@classhook@topend}
% 例如：\section 
\def\title@classkeys@normal{}
\def\title@classinitial@normal{}
\def\title@classhook@normalbegin{}
\def\title@classhook@normalend{\title@floatbarrier@hook}
\def\title@classhook@normalafterdef{}
\let\title@floatbarrier@hook\@empty 
\protected\def\title@class@normal #1#2{%
  \title@iffloatbarrier
    {\FloatBarrier
      \gdef\title@floatbarrier@hook{\@fb@topbarrier 
        \gdef\title@floatbarrier@hook{}}}
    {}%
  \if@noskipsec \leavevmode \fi 
  \par 
  \title@ifafterindent{\@afterindenttrue}{\@afterindentfalse}%
  \if@nobreak \title@classhook@normalbegin \everypar{}%
  \else 
    \title@break
    \title@classhook@normalbegin 
    \title@setheadingskip{before}%
    \title@iffixskip{\title@fixtopskip}{}%
    \title@ifensureskip{\hrule\@height\z@\relax \par}{}%
    \addvspace{\title@headingskip}%
  \fi 
  \title@ifstar{#2}{\title@class@normal@@}{\title@class@normal@}{#1}}
\protected\def\title@class@normal@ #1#2#3{%
  \ifnum\title@getlevel>\c@secnumdepth
    \title@ifnamefalse
    \title@makeanchor{#1}%
    \let\@svsec\@empty 
  \else 
    \title@ifnumbering
      {\title@ifnametrue \refstepcounter{#1}%
        \protected@edef\@svsec{\title@name\title@aftername}\relax}
      {\title@ifnamefalse \title@makeanchor{#1}%
        \let\@svsec\@empty}%
  \fi 
  \title@beforerecord
  \title@gettitle{#2}%
  \title@addtocline{#2}%
  \title@extras
  \title@mark{#2}%
  \title@ifrunin
    {\def\@svsechd{%
      \normalfont\title@format{%
        {\title@indentbox{\title@getindent}}\@svsec 
        \title@titleformat{#3}%
        \title@aftertitle}}}
    {\begingroup
      \title@heading@format@initial
      \title@format{%
        \title@headinghang{\@svsec}%
        \title@titleformat{#3}%
        \title@aftertitle}\par 
      \endgroup}%
  \title@class@endnormal{#1}}
\protected\def\title@class@normal@@ #1#2#3{%
  \title@makeanchor{#1}%
  \title@ifnamefalse 
  \title@beforerecord
  \title@gettitle{#2}%
  \title@extras
  \title@ifrunin
    {\def\@svsechd{%
      \normalfont\title@format{%
        {\title@indentbox{\title@getindent}}%
        \title@titleformat{#3}%
        \title@aftertitle}}}
    {\begingroup
      \title@heading@format@initial 
      \title@format{%
        \title@headinghang{}%
        \title@titleformat{#3}%
        \title@aftertitle}\par 
      \endgroup}%
  \title@class@endnormal{#1}}
\protected\def\title@class@endnormal #1{%
  \title@ifrunin
    {\@nobreakfalse 
      \global\@noskipsectrue
      \everypar{%
        \if@noskipsec 
          \global\@noskipsecfalse 
          {\setbox\z@\lastbox}%
          \clubpenalty\@M 
          \begingroup \@svsechd \endgroup
          \unskip 
          \begingroup \title@setheadingskip{after}%
            \unless\ifdim\title@headingskip=\z@ \hskip\title@headingskip \fi 
          \endgroup 
        \else \clubpenalty\@clubpenalty \everypar{}\fi}}
    {\par\nobreak 
      \title@setheadingskip{after}%
      \title@iffixskip{\title@fixheadingskip}{}%
      \vskip\title@headingskip 
      \@afterheading}%
  \title@classhook@normalend 
  \ignorespaces}
\def\title@classkeys@free{}
\def\title@classinitial@free{}
\def\title@classhook@freebegin{}
\def\title@classhook@freeend{}
\def\title@classhook@freeafterdef{}
\protected\def\title@class@free #1#2{%
  \title@iffloatbarrier
    {\FloatBarrier
      \gdef\title@floatbarrier@hook{\@fb@topbarrier 
        \gdef\title@floatbarrier@hook{}}}
    {}%
  \if@noskipsec \leavevmode \fi
  \par 
  \title@ifafterindent{\@afterindenttrue}{\@afterindentfalse}%
  \if@nobreak \title@classhook@freebegin \everypar{}%
  \else 
    \title@break
    \title@classhook@freebegin 
    \title@setheadingskip{before}%
    \title@iffixskip{\title@fixtopskip}{}%
    \title@ifensureskip{\hrule\@height\z@\relax \par}{}%
    \addvspace{\title@headingskip}%
  \fi 
  \title@ifstar{#2}{\title@class@free@@}{\title@class@free@}{#1}}
\protected\def\title@class@free@ #1#2#3{%
  \ifnum\title@getlevel>\c@secnumdepth 
    \title@ifnamefalse 
    \title@makeanchor{#1}%
    \let\@svsec\@empty 
  \else 
    \title@ifnumbering
      {\title@ifnametrue \refstepcounter{#1}%
        \protected@edef\@svsec{\title@name\title@aftername}\relax}
      {\title@ifnamefalse \title@makeanchor{#1}%
        \let\@svsec\@empty}%
  \fi
  \title@beforerecord
  \title@gettitle{#1}%
  \title@addtocline{#2}%
  \title@extras
  \title@mark{#2}%
  \title@heading@format@initial 
  {\title@format{%
    \@svsec
    \title@titleformat{#3}%
    \title@aftertitle}}%
  \title@class@endfree{#1}}
\protected\def\title@class@free@@ #1#2#3{%
  \title@ifnamefalse 
  \title@makeanchor{#1}%
  \title@beforerecord
  \title@gettitle{#2}%
  \title@extras
  \title@heading@format@initial 
  {\title@format{%
    \@svsec
    \title@titleformat{#3}%
    \title@aftertitle}}%
  \title@class@endfree{#1}}
\protected\def\title@class@endfree #1{%
  \ifvmode 
    \title@setheadingskip{after}%
    \title@iffixskip{\title@fixheadingskip}{}%
    \vskip\title@headingskip 
    \@afterheading 
  \fi 
  \title@classhook@freeend 
  \ignorespaces}
\def\title@classkeys@wrap{}
\def\title@classinitial@wrap{}
\def\title@classhook@wrapbegin{}
\def\title@classhook@wrapend{}
\def\title@classhook@wrapafterdef{}
\protected\def\title@class@wrap #1#2#3#4{%
  \title@iffloatbarrier
    {\FloatBarrier
      \gdef\title@floatbarrier@hook{\@fb@topbarrier 
        \gdef\title@floatbarrier@hook{}}}
    {}%
  \if@noskipsec \leavevmode \fi
  \par 
  \title@ifafterindent{\@afterindenttrue}{\@afterindentfalse}%
  {\title@ifmeasuringtrue 
    \title@setvarwidth\@tempboxa{.4\textwidth}{%
      \title@ifstar{#2}{%
        \title@ifnamefalse 
        \title@makeanchor{#1}%
        \let\@svsec\@empty}{%
        \ifnum\title@getlevel>\c@secnumdepth 
          \title@ifnamefalse 
          \title@makeanchor{#1}%
          \let\@svsec\@empty
        \else 
          \title@ifnumbering
            {\title@ifnametrue \refstepcounter{#1}%
              \protected@edef\@svsec{\title@name\title@aftername}\relax}
            {\title@ifnamefalse \title@makeanchor{#1}%
              \let\@svsec\@empty}%
        \fi 
      }%
      \title@beforerecord
      \title@gettitle{#2}%
      \title@extras
      \title@heading@format@initial 
      \title@format{%
        \@svsec
        \title@titleformat{#4}%
        \title@aftertitle}\par\@@par
      \title@setheadingskip{after}%
      \title@iffixskip{\title@fixheadingskip}{}%
      \vskip\title@headingskip}%
    \global\setbox\@ne\vtop{\unvbox\@tempboxa}}
  \@tempdima\ht\@ne \advance\@tempdima\dp\@ne 
  \@tempdimb\@tempdima \advance\@tempdimb\baselineskip
  \divide\@tempdima\baselineskip \count@\@tempdima 
  \advance\count@
    \ifdim\@tempdimb<\the\count@.5\baselineskip\@ne\else\tw@\fi
  \setbox\title@box\hbox{%
    \title@setheadingskip{left}\hskip\title@headingskip
    \raise\fpeval{.825*\f@size}\p@ \hbox{\box\@ne}%
    \title@setheadingskip{right}\hskip\title@headingskip}%
  \dp\title@box\z@ 
  \needspace{\@tempdimb}%
  \title@ifmeasuringfalse
  \if@nobreak \title@classhook@wrapbegin \everypar{}%
  \else 
    \title@break
    \title@classhook@wrapbegin 
    \title@setheadingskip{before}%
    \title@iffixskip{\title@fixtopskip}{}%
    \title@ifensureskip{\hrule\@height\z@\relax \par}{}%
    \addvspace{\title@headingskip}%
  \fi 
  \title@mark{#3}%
  \title@addtocline{#3}%
  \hangindent\wd\title@box \hangafter-\count@ 
  \noindent\llap{\box\title@box}%
  \cus@ignorepars} % 这里暂时使用 \ignorepars，另见 titlesec 的 \ttlhx@wrap

\ifdefined\if@openright \else\ExpandArgs{c}\newif{if@openright}\@openrightfalse\fi
\ifdefined\if@mainmatter \else\ExpandArgs{c}\newif{if@mainmatter}\@mainmattertrue\fi
\def\chaptermark#1{\markboth{\MakeUppercase{\titleifnamed{\titlethechapter\ }{}#1}}{}}
\def\sectionmark#1{\markright{\MakeUppercase{\titleifnamed{\titlethesection\ }{}#1}}}
\definetitle\part[page]{
  name={第,部分},
  number=\Roman{part},
  format=\huge\bfseries\centering,
  aftername=\par\vskip 20pt,
  aftertitle=\par,
  beforeskip=0pt plus 1fil,
  afterskip=0pt plus 1fil,
  break=\if@openright\cleardoublepage\else\clearpage\fi,
  tocline=\titleifnamed{\titlethepart\hspace{1em}}{}#2,
}
\definetitle\chapter[top]{
  name={第,章},
  number=\zhnum{chapter},
  format=\huge\bfseries\centering,
  aftername=\quad,
  aftertitle=\par,
  beforeskip=50pt,
  afterskip=40pt,
  break=\if@openright\cleardoublepage\else\clearpage\fi,
  afterindent=true,
  tocline=\titleifnamed{\protect\numberline{\titlethechapter\hspace{.3em}}}{}#2,
  mark=\chaptermark,
}
\newcommand\titlenumberline[1]%
  {\titleifnamed{\protect\numberline{\@nameuse{titlethe#1}}}{}}
\definetitle\section{
  level=section,
  number=\arabic{section},
  format=\Large\bfseries\centering,
  aftername=\quad,
  aftertitle=\@@par,
  hang=true,
  beforeskip=3.5ex plus 1ex minus .2ex,
  afterskip=2.3ex plus .2ex,
  break=\addpenalty{\@secpenalty},
  afterindent=true,
  tocline=\titlenumberline{#1}#2,
  mark=\sectionmark,
}
\definetitle\subsection{
  level=subsection,
  number=\thesection.\arabic{subsection},
  format=\large\bfseries,
  aftername=\quad,
  aftertitle=\@@par,
  hang=true,
  beforeskip=3.25ex plus 1ex minus .2ex,
  afterskip=1.5ex plus .2ex,
  break=\addpenalty{\@secpenalty},
  afterindent=true,
  tocline=\titlenumberline{#1}#2,
}
\definetitle\subsubsection{
  level=subsubsection,
  number=\thesubsection.\arabic{subsubsection},
  format=\normalsize\bfseries,
  aftername=\quad,
  aftertitle=\@@par,
  hang=true,
  beforeskip=3.25ex plus 1ex minus .2ex,
  afterskip=1.5ex plus .2ex,
  break=\addpenalty{\@secpenalty},
  afterindent=true,
  tocline=\titlenumberline{#1}#2,
}
\definetitle\paragraph{
  level=paragraph,
  number=\thesubsubsection.\arabic{paragraph},
  format=\normalsize\bfseries,
  aftername=\quad,
  runin=true,
  beforeskip=3.25ex plus 1ex minus .2ex,
  afterskip=1em,
  break=\addpenalty{\@secpenalty},
  afterindent=true,
  tocline=\titlenumberline{#1}#2,
}
\definetitle\subparagraph{
  level=subparagraph,
  number=\theparagraph.\arabic{subparagraph},
  format=\normalsize\bfseries,
  aftername=\quad,
  runin=true,
  indent=\parindent,
  beforeskip=3.25ex plus 1ex minus .2ex,
  afterskip=1em,
  break=\addpenalty{\@secpenalty},
  afterindent=true,
  tocline=\titlenumberline{#1}#2,
}

\ExplSyntaxOn

\hook_new:n { frontmatter }
\hook_new:n { mainmatter }
\hook_new:n { backmatter }
\DeclareDocumentCommand \frontmatter { O{} } 
  {
    \cleardoublepage
    \@mainmatterfalse 
    \setuptitle[0]{mode=nonumber}
    \pagenumbering{roman}
    \hook_use_once:n { frontmatter }
  }
\DeclareDocumentCommand \mainmatter { O{} } 
  {
    \cleardoublepage
    \@mainmattertrue 
    \setuptitle[0]{mode=normal}
    \pagenumbering{arabic}
    \hook_use_once:n { mainmatter }
  }
\DeclareDocumentCommand \backmatter { O{} } 
  {
    \if@openright
      \cleardoublepage
    \else
      \clearpage
    \fi
    \@mainmatterfalse
    \setuptitle[0]{mode=nonumber}
    \hook_use_once:n { backmatter }
  }
